Root element is missing

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Im reading the XML from file system and loading into XDocument using MemoryStream. But im getting root element is missing error.<br/>
Here is code
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; <?<span style="color:#A31515; xml <span style="color:Red; version<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 1.0<span style="color:Black; " <span style="color:Red; encoding<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; utf-8<span style="color:Black; "<span style="color:Blue; ?>
<span style="color:Blue; <<span style="color:#A31515; Root <span style="color:Red; xmlns:xsi<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; http://www.w3.org/2001/XMLSchema-instance<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; FormInfo<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Id<span style="color:Blue; >Id1<span style="color:Blue; </<span style="color:#A31515; Id<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; FormInfo<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; Root<span style="color:Blue; >
[/code]
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; private <span style="color:Blue; byte[] GetFile()
{
<span style="color:Blue; byte[] buffer = <span style="color:Blue; null;
<span style="color:Blue; using (FileStream fs = <span style="color:Blue; new FileStream(<span style="color:#A31515; @"C:MappersTest.xml", FileMode.Open))
{
fs.Position = 0;
buffer = <span style="color:Blue; new <span style="color:Blue; byte[fs.Length];
fs.Read(buffer,0,(<span style="color:Blue; int)fs.Length);
}
}


<span style="color:Blue; private <span style="color:Blue; void DoWork()
{
<span style="color:Blue; byte[] xml = GetFile();


<span style="color:Blue; using (MemoryStream xmlDataStream = <span style="color:Blue; new MemoryStream())
{
xmlDataStream.Write(xml, 0, xml.Length);

<span style="color:Green; //Its failing Here with error...."Root element is missing"
XDocument xDoc = XDocument.Load(xmlDataStream);

<span style="color:Green; // Add processing instructions
xDoc.AddFirst(GetXslStyleSheetProcessingInstruction());

<span style="color:Green; // Save the stream
xDoc.Save(xmlDataStream);

<span style="color:Green; // Reset the stream to begining
xmlDataStream.Position = 0;

}
}
[/code]
<br/>
<br/>


View the full article
 
Back
Top