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;
UPDATE 3
Using TToni's suggestion and replacing double single quotes:
... Now I get the whole line in my server method:

... 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.