How can I parse this XML (without getting the "Root" or "Sequence contains no elements" element)?

This is a branch of this question. Why is the HttpWebRequest body null after "switching to Rubicon"? which was answered (one obstacle is jumping), but the next obstacle is touching me.

With this code:

public async void PostArgsAndXMLFileAsStr([FromBody] string stringifiedXML, string serialNum, string siteNum) { XDocument doc = XDocument.Parse(await Request.Content.ReadAsStringAsync()); 

... or that:

 XDocument doc = XDocument.Load(await Request.Content.ReadAsStreamAsync()); 

... and this is like incoming stringifiedXML:

 <?xml version=1.0?> <LocateAndLaunch> <Tasks>Some Task</Tasks> <Locations>Some Location</Locations> </LocateAndLaunch> 

... I get an exception: "System.Xml.XmlException was not handled by user code HResult = -2146232000 Message = root element missing."

Using this code (same stringifiedXML):

 XDocument doc = XDocument.Parse(stringifiedXML); 

... I get

System.InvalidOperationException was not handled by user code HResult = -2146233079 Message = Sequence does not contain elements Source = System.Core StackTrace: at System.Linq.Enumerable.First [TSource] (source IEnumerable`1) in HandheldServer.Controllers.DeliveryItemsController.d__2 .MoveNext () in C: \ HandheldServer \ HandheldServer \ Controllers \ DeliveryItemsController.cs: line 109 InnerException:

IOW, depending on how I parse the input string, I get either β€œ Missing Root Element ” or Sequence Does Not Contain Elements ”

What is Deuce McAlistair MacLean Virginia Weeper?!? Is <LocateAndLaunch> root element? Not elements of Some Task and Some Location ?

Do I need to manually separate XML without being able to use XDocument or such?

Note. From Fiddler, body data is sent (XML text) (which I send, as I hope); but even then, the XML parsing code on the server fails.

An attempt to send the same data from the client code of the PDA / Compact Framework leads to the fact that the data is transmitted only through "=" (something like <xml version=" , transmitted to the server and then on the client, I see" This operation does not can be executed after sending the request "

UPDATE

So, based on Marcin's answer, it seems that if I stick with XDocument.Parse (), I should be fine, and based on TToni's answer, the data / xml file itself may be bad. Here is the contents of the file:

 <?xml version="1.0"?> <LocateAndLaunch> <Tasks> </Tasks> <Locations> </Locations> </LocateAndLaunch> 

So, there are quotes around the version number, but I need to somehow avoid them - that’s why the data is truncated - because when he sees the first quote (to "1.0"), she thinks this line is possible?

UPDATE 2

Marcin, the incoming xml data (string) is truncated before it ever gets into the XDocument code:

 [Route("api/DeliveryItems/PostArgsAndXMLFileAsStr")] public async void PostArgsAndXMLFileAsStr([FromBody] string stringifiedXML, string serialNum, string siteNum) { string beginningInvoiceNum = string.Empty; string endingInvoiceNum = string.Empty; //XDocument doc = XDocument.Parse(stringifiedXML); //XDocument doc = XDocument.Parse(await Request.Content.ReadAsStringAsync()); XDocument doc = XDocument.Load(await Request.Content.ReadAsStreamAsync()); 

UPDATE 3

Using TToni's suggestion and replacing double single quotes:

... Now I get the whole line in my server method:

enter image description here

... but I still get the " Root element is missing ":

System.Xml.XmlException was not handled by user code HResult = -2146232000 Message = no root element. Source = System.Xml LineNumber = 0 LinePosition = 0 SourceUri = "" Stack traces: in System.Xml.XmlTextReaderImpl.Throw (exception e) in System.Xml.XmlTextReaderImpl.ParseDocumentContent () in System.Xml.XmlTextReaderImpl.Read () in System.Xml.Linq.XDocument.Load (XmlReader reader, LoadOptions options) in System.Xml.Linq.XDocument.Load (stream stream, LoadOptions parameters) in System.Xml.Linq.XDocument.Load (stream stream) in HandheldServer .Controllers.DeliveryItemsController.d__2.MoveNext () in c: \ HandheldServer \ HandheldServer \ Controllers \ DeliveryItemsController.cs: line 66 InnerException:

If I were not crazy, it would drive me crazy. Be that as it may, I do not know where they are leading me. Many employers are looking for someone to manage; maybe this is what they mean?

UPDATE 4

Using this:

 XDocument doc = XDocument.Parse(stringifiedXML); 

... instead of this:

 XDocument doc = XDocument.Load(await Request.Content.ReadAsStreamAsync()); 

... decided that the "Root element is missing" err msg; thus, I marked Marcin's answer as correct.

+7
c # linq-to-xml
source share
4 answers

The XML example that you provided works fine with XDocument :

 var stringifiedXML = @"<?xml version=""1.0""?> <LocateAndLaunch> <Tasks>Some Task</Tasks> <Locations>Some Location</Locations> </LocateAndLaunch>"; XDocument doc = XDocument.Parse(stringifiedXML); var tasks = (string)doc.Root.Element("Tasks"); var locations = (string)doc.Root.Element("Locations"); 

So your problem is with await Request.Content.ReadAsStringAsync() , not with XDocument .

Make sure ReadAsStringAsync() returns what you need.

+6
source share

For those who find this page through Google, but are still confused. I believe I found the answer thanks to a similar MS Post .

It consists in reloading the stream before using it:

 xmlStream.Seek(0, SeekOrigin.Begin); 

Without this, before reading, you essentially start loading at the end of the stream. There is no root element because there is no more flow.

+6
source share

If this is really your gated XML, your XML declaration is broken. You must enclose the version number in single or double quotation marks, so instead of <?xml version=1.0?> Do <?xml version="1.0"?> Or <?xml version='1.0'?> . It also explains IMO errors.

XML declaration syntax: http://www.w3.org/TR/REC-xml/#NT-XMLDecl

+2
source share

this can be achieved easily by setting the binding parameters as indicated in the blog post below. I tried in one of my projects where I had to use raw xml post for WebApi

Check out this blog post

+1
source share

All Articles