Re: There is an error in XML document (2, 2).

  • Thread starter Thread starter Jon Skeet [C# MVP]
  • Start date Start date
J

Jon Skeet [C# MVP]

Sean said:
When I deserialize the following xml file; I get the error "There is an error
in XML document (2, 2)." What am I doing wrong?

For a start, you're not looking at the inner exception - if you did,
you'd see more information:

Unhandled Exception: System.InvalidOperationException: There is an
error in XML document (2, 2).
---> System.InvalidOperationException: <MylTest xmlns=''> was not
expected.
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderTes
tData.Read3_TestData()
--- End of inner exception stack trace ---
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader
xmlReader, String encodingStyle, XmlDeserializationEvents events)
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader
xmlReader, String encodingStyle)
at System.Xml.Serialization.XmlSerializer.Deserialize(Stream stream)
at Test.Main()

Now that gives a hint - it's the root element. Consider that you've
asked it to deserialize "TestData" and the root element is "MylTest":
unless you've got something cunning somewhere in code you haven't shown
us, the element should be called TestData. Changing it to TestData
fixes the problem in my test app.
 
Jon,
Thanks; it really helped.

Jon Skeet said:
For a start, you're not looking at the inner exception - if you did,
you'd see more information:

Unhandled Exception: System.InvalidOperationException: There is an
error in XML document (2, 2).
---> System.InvalidOperationException: <MylTest xmlns=''> was not
expected.
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderTes
tData.Read3_TestData()
--- End of inner exception stack trace ---
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader
xmlReader, String encodingStyle, XmlDeserializationEvents events)
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader
xmlReader, String encodingStyle)
at System.Xml.Serialization.XmlSerializer.Deserialize(Stream stream)
at Test.Main()

Now that gives a hint - it's the root element. Consider that you've
asked it to deserialize "TestData" and the root element is "MylTest":
unless you've got something cunning somewhere in code you haven't shown
us, the element should be called TestData. Changing it to TestData
fixes the problem in my test app.
 
Back
Top