Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.net: how do debug XmlSerializer.Deserialize errors?

During development I've seen xml read errors like this more than once:

TestData.ReadFromXml: xml Deserialize error:There is an error in XML document (2, 2)..

What exactly does (2, 2) refer to? Is it line 2 in the xml file? Line 2, token 2, what?

Are there any debug options I can add to shed more light on the problem?

Edit: here are the first 2 lines:

<?xml version="1.0" encoding="utf-8"?>
<TestSession xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

So it would be complaining on the 'T' character on the 2nd line? This is a class I just serialized, and it is well formed. When deserializing, I think it doesn't like something in the class... and that might explain why it stops at the T in TestSession. It would be nice if it told you what it didn't like.

like image 270
P a u l Avatar asked Apr 08 '26 00:04

P a u l


2 Answers

It's (line, character) and begins at 1 (not 0 based).

You should examine the InnerException to get a more precise error message. Looking at your example, it could be something like:

"<TestSession> was not expected."

You can also change the serialized name by applying XmlRootAttribute to your class.

like image 65
Mehmet Ergut Avatar answered Apr 09 '26 15:04

Mehmet Ergut


It's line 2, character 2.

I recommend you open the XML file in Visual Studio and then look at the Errors window to see if it complains at all.

like image 43
John Saunders Avatar answered Apr 09 '26 16:04

John Saunders



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!