"There is an error in XML document (0, 0)." error while XML Deserialization
-
Wednesday, February 06, 2008 4:32 AMHi
I am trying experimenting with XML serialization and deserialization using XSD.exe to generate the classes for me from my XML schema. The schema is pretty straight forward and is as follows
<?xml version="1.0" encoding="utf-8" ?>
<xsd: schema xmlns: xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace ="http://tryingXML.prasad.org/exampel2"
xmlns:tns="http://tryingXML.prasad.org/exampel2">
<xsd:element name="Properties" type ="tns: PropertyType"/>
<xsd: complexType name ="PropertyType" >
<xsd: sequence>
<xsd: element name ="KeyValuePair" nillable ="false" type ="tns: KeyValue"/>
<xsd: element name ="ArrayOfElements" type ="tns: ArrayType" />
</xsd: sequence>
</xsd: complexType>
<xsd: complexType name ="KeyValue">
<xsd: sequence>
<xsd: element name="Key" type ="xsd: string"/>
<xsd: element name="Value" type ="xsd: string"/>
</xsd: sequence>
</xsd: complexType>
<xsd: complexType name ="ArrayType">
<xsd: sequence minOccurs ="0" maxOccurs ="unbounded">
<xsd: element name ="ArrayElement" type ="tns: ElementType"/>
</xsd: sequence>
</xsd: complexType>
<xsd: complexType name ="ElementType">
<xsd: sequence>
<xsd: element name ="serverName" type ="xsd: string"/>
<xsd: element name ="Instance" type ="xsd: string" />
<xsd: element name ="IntegratedAuth" type ="xsd: string"/>
</xsd: sequence>
</xsd: complexType>
</xsd: schema>
I wrote following code to serialize / deserialize XML data for above schema. Following is the sample code.
XmlSerializer serialize = new XmlSerializer(typeof(PropertyType), "http://tryingXML.prasad.org/exampel2");
PropertyType property = new PropertyType();
property.KeyValuePair = new KeyValue();
property.KeyValuePair.Key = "Test";
property.KeyValuePair.Value = "TestValue";
ElementType[] arrElems = new ElementType[] { new ElementType { Instance ="TestServer", IntegratedAuth ="SSPI", serverName ="TST"}
};
property.ArrayOfElements = new ArrayType();
property.ArrayOfElements.ArrayElement = arrElems;
FileStream stream = new FileStream("Output.xml",FileMode.Create);
serialize.Serialize(stream, property);
PropertyType ptype = (PropertyType)serialize.Deserialize(stream);
It serializes the values I am specifying to the XML file, but when I try to deseriaize the same file I get an error
"There is an error in XML document (0, 0)." with an inner exception of "Root Element is missing".
Let me know if I need to do anything else for this.
Thanks
Prasad
All Replies
-
Wednesday, February 06, 2008 5:08 PM
After you have written to the stream it is positioned at the end, after the XML you have written. If you then try to read from that same stream there is no root element to be found. So you need to reposition the stream (stream.Position = 0) if the stream supports that or you need to close the stream once you have wriiten to it and open a new one for reading.
-
Wednesday, February 06, 2008 10:55 PMThanks Martin It worked
-
Monday, September 15, 2008 10:27 PM
This is very helpful. Thanks.- Proposed As Answer by Sean The Funky Homosapien Tuesday, May 07, 2013 12:01 PM
-
Monday, January 26, 2009 12:02 PMIt was simple, but anyway it did do the trick....
Thanks
-
Wednesday, July 01, 2009 1:21 PMReally it is very helpful. Thanks..
-
Thursday, November 19, 2009 2:28 PMit worked ! Thanks
-
Sunday, January 17, 2010 7:22 AMThanks Martin, very helpful tip.
-
Tuesday, October 05, 2010 11:54 PM
After you have written to the stream it is positioned at the end, after the XML you have written. If you then try to read from that same stream there is no root element to be found. So you need to reposition the stream (stream.Position = 0) if the stream supports that or you need to close the stream once you have wriiten to it and open a new one for reading.
Thanks for your help! I encountered the same problem. -
Wednesday, February 08, 2012 4:03 PM
Thank You v much Martin Honnen Stream.Position = 0 is the solution to this scary problem

