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

  • Thread starter Jon Skeet [C# MVP]
  • 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.
 
G

Guest

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top