title: How can I parse this XML (without getting "Root element is missing" or "Sequence contains no elements")?
body: <p>This is an offshoot from this question <a href="https://stackoverflow.com/questions/22358231/why-is-the-httpwebrequest-body-val-null-after-crossing-the-rubicon">Why is the HttpWebRequest body val null after &quot;crossing the Rubicon&quot;?</a> which was answered (one hurdle is leapt), but the next hurdle trips me up.</p>
  
  <p>With this code:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public async void PostArgsAndXMLFileAsStr([FromBody] string stringifiedXML, string serialNum, string siteNum)
  {
      XDocument doc = XDocument.Parse(await Request.Content.ReadAsStringAsync()); 
  </code></pre>
  
  <p>...or this:</p>
  
  <pre class="lang-cs prettyprint-override"><code>XDocument doc = XDocument.Load(await Request.Content.ReadAsStreamAsync());
  </code></pre>
  
  <p>...and this as the incoming stringifiedXML:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version=1.0?&gt;
  &lt;LocateAndLaunch&gt;
      &lt;Tasks&gt;Some Task&lt;/Tasks&gt;
      &lt;Locations&gt;Some Location&lt;/Locations&gt;
  &lt;/LocateAndLaunch&gt;
  </code></pre>
  
  <p>...I get the exception: "<em>System.Xml.XmlException was unhandled by user code HResult=-2146232000 Message=Root element is missing.</em>"</p>
  
  <p>With this code (same stringifiedXML):</p>
  
  <pre class="lang-cs prettyprint-override"><code>XDocument doc = XDocument.Parse(stringifiedXML);
  </code></pre>
  
  <p>... I get</p>
  
  <blockquote>
    <p>System.InvalidOperationException was unhandled by user code
    HResult=-2146233079 Message=Sequence contains no elements
    Source=System.Core StackTrace: at
    System.Linq.Enumerable.First[TSource](IEnumerable`1 source) at
    HandheldServer.Controllers.DeliveryItemsController.d__2.MoveNext() in
    c:\HandheldServer\HandheldServer
    \Controllers\DeliveryItemsController.cs:line 109 InnerException:</p>
  </blockquote>
  
  <p>IOW, depending on how I parse the incoming string, I get either "<strong><em>Root element is missing</em></strong>" or "<strong><em>Sequence contains no elements</em></strong>"</p>
  
  <p>What the Deuce McAlistair MacLean Virginia Weeper?!? Isn't <code>&lt;LocateAndLaunch&gt;</code> a root element? Aren't <code>Some Task</code> and <code>Some Location</code> elements?</p>
  
  <p>Will I need to manually take the XML apart without being able to use <code>XDocument</code> or such?</p>
  
  <p>Note: From Fiddler, sending the body data (XML text) works (what I send arrives as I would hope); but even then, the XML parse code on the server fails.</p>
  
  <p>Trying to send the same data from the handheld/Compact Framework client code, though, results in just the data through the "=" (something like <code>&lt;xml version="</code> being passed to the server; and then on the client, I see "<em>This operation cannot be performed after the request has been submitted</em>"</p>
  
  <h2>UPDATE</h2>
  
  <p>So based on Marcin's answer, it seems if I stick with XDocument.Parse() I should be okay, and based on TToni's answer, the data/xml itself may be bad. Here is the contents of the file:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;LocateAndLaunch&gt;
      &lt;Tasks&gt;
      &lt;/Tasks&gt;
      &lt;Locations&gt;
      &lt;/Locations&gt;
  &lt;/LocateAndLaunch&gt;
  </code></pre>
  
  <p>So there are quotes around the version number - but do I need to escape these somehow - is this why the data is being truncated - because when it sees the first quote (before the "1.0") it thinks that is the string termination, maybe?</p>
  
  <h2>UPDATE 2</h2>
  
  <p>Marcin, the incoming "xml" data (string) is truncated before it ever gets to the XDocument code:</p>
  
  <pre><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());
  </code></pre>
  
  <h2>UPDATE 3</h2>
  
  <p>By using TToni's suggestion and replacing the double with single quotes:</p>
  
  <p>...I'm now getting the whole string into my server method:</p>
  
  <p><img src="https://i.stack.imgur.com/7TNWW.png" alt="enter image description here"></p>
  
  <p>...but I'm still getting "<strong><em>Root element is missing</em></strong>":</p>
  
  <p><em>System.Xml.XmlException was unhandled by user code
    HResult=-2146232000
    Message=Root element is missing.
    Source=System.Xml
    LineNumber=0
    LinePosition=0
    SourceUri=""
    StackTrace:
         at System.Xml.XmlTextReaderImpl.Throw(Exception e)
         at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
         at System.Xml.XmlTextReaderImpl.Read()
         at System.Xml.Linq.XDocument.Load(XmlReader reader, LoadOptions options)
         at System.Xml.Linq.XDocument.Load(Stream stream, LoadOptions options)
         at System.Xml.Linq.XDocument.Load(Stream stream)
         at HandheldServer.Controllers.DeliveryItemsController.d__2.MoveNext() in c:\HandheldServer\HandheldServer\Controllers\DeliveryItemsController.cs:line 66
    InnerException:</em> </p>
  
  <p>If I wasn't already crazy, this would drive me crazy. As it is, I don't know quite where I'm being driven. Many employers are looking for somebody who is driven; maybe this is what they mean?</p>
  
  <h2>UPDATE 4</h2>
  
  <p>Using this:</p>
  
  <pre><code>XDocument doc = XDocument.Parse(stringifiedXML);
  </code></pre>
  
  <p>...instead of this:</p>
  
  <pre><code>XDocument doc = XDocument.Load(await Request.Content.ReadAsStreamAsync());
  </code></pre>
  
  <p>...solved the "Root element is missing" err msg; thus, I marked Marcin's answer as correct.</p>