title: Loading uploaded xml file into XmlDocument object
body: <p>Anyway, I'm trying to upload a file from the browser and then read it into an XmlDocument object on the server.  Originally I cracked this by saving the file to disk, reading it into the XmlDocument object and then deleting the file.  The only problem was that the delete action was trying to take place before the <code>XmlDocument.Load</code> action had completed.
  It felt like an ugly solution anyway, so it was happily abandoned.</p>
  
  <p>Next effort was to read directly from the <code>Request.Files[x].InputStream</code> directly into the XmlDocument, but I'm having problems.
  The following code fails with a </p>
  
  <blockquote>
    <p>'Root element is missing'</p>
  </blockquote>
  
  <p>I know the XML is valid so it must be something else.</p>
  
  <pre><code>foreach (string file in Request.Files) 
  {
      HttpPostedFileBase postedFile = Request.Files[file] as  HttpPostedFileBase;
  
      if (postedFile.ContentLength &gt; 0) //if file not empty
      {
         //create an XML object and load it in
          XmlDocument xmlProjPlan = new XmlDocument();
  
          Stream fileStream = postedFile.InputStream;
          byte[] byXML = new byte[postedFile.ContentLength];
          fileStream.Read(byXML, 0, postedFile.ContentLength);
          xmlProjPlan.Load(fileStream);
       }
  }
  </code></pre>
  
  <hr>