none
Root element is missing

    Question

  • I'm reading the XML from file system and loading into XDocument using MemoryStream. But i'm getting root element is missing error.
    Here is code

    <?xml version="1.0" encoding="utf-8"?>
    <Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
     <FormInfo>
      <Id>Id1</Id>
     </FormInfo>
    </Root>
    
    private byte[] GetFile()
    {
    	  byte[] buffer = null;
          using (FileStream fs = new FileStream(@"C:\Mappers\Test.xml", FileMode.Open))
          {
            fs.Position = 0;
            buffer = new byte[fs.Length];
            fs.Read(buffer,0,(int)fs.Length);
          }
    }
    
    
    private void DoWork()
    {
    	byte[] xml = GetFile();
          
          
          using (MemoryStream xmlDataStream = new MemoryStream())
          {
            xmlDataStream.Write(xml, 0, xml.Length);
    
    		//Its failing Here with error...."Root element is missing"
            XDocument xDoc = XDocument.Load(xmlDataStream);
    
            // Add processing instructions      
            xDoc.AddFirst(GetXslStyleSheetProcessingInstruction());
    
            // Save the stream
            xDoc.Save(xmlDataStream);
    
            // Reset the stream to begining
            xmlDataStream.Position = 0;
    
          }
    }
    



    Thursday, July 07, 2011 3:18 PM

Answers

All replies

  • After doing

      xmlDataStream.Write(xml, 0, xml.Length);

    you need to set

      xmlDataStream.Position = 0;

    so that the Load call that follows reads from the beginning of your stream.


    MVP Data Platform Development My blog
    Thursday, July 07, 2011 3:43 PM


  • Thanks that worked.  I have a question about using memory stream, not sure if i should ask here.
    the Xml data could be large, so when i load XDocument with xml data, now i have three copies of XML that occupies the memory.
    one in xml variable
    second in xmlDataStream
    third in xDocument

    If i set xml = null; will that clears the memory immediately?
    Is there any way to Add processing instruction without loading XDocument?




    Thursday, July 07, 2011 5:44 PM


  • Thanks that worked.  I have a question about using memory stream, not sure if i should ask here.
    the Xml data could be large, so when i load XDocument with xml data, now i have three copies of XML that occupies the memory.
    one in xml variable
    second in xmlDataStream
    third in xDocument

    If i set xml = null; will that clears the memory immediately?
    Is there any way to Add processing instruction without loading XDocument?




    Well there certainly is no need to use a byte array buffer to load an XML document from the file system into an XDocument, you can simply do

      XDocument doc = XDocument.Load(@"C:\Mappers\Test.xml");

    As for memory clearing or releasing, managed .NET code is run with garbage collection which is described in http://msdn.microsoft.com/en-us/library/0xy59wtx.aspx.


    MVP Data Platform Development My blog
    Friday, July 08, 2011 10:51 AM