An extremely undescriptive error message took hours of my valuable time. Of course, we are talking about XmlSerializer trying to parse an XML file. What actually happened was that an inherited piece of code caught and ignored this error, while I’ve configured my Visual Studio to break on System exceptions, so it appeared to be a cause of another error!
Anyway – the error itself is still far from descriptive, and it deserves a bit of clarification.
XmlSerializer serializer_ = new XmlSerializer(typeof(MyDataType));
StreamReader reader_ = new StreamReader(filename);
MultiLevelReportDescriptor desc_ = (MyDataType)serializer_.Deserialize(reader_);
This benign piece of code assumes that root element in the XML file is called MyDataTape:
<?xml version="1.0" encoding="utf-8"?>
<MyDataType>
...
Unfortunately, instead of telling the unlucky developer this (notorious) fact, there goes the mystification:
{"There is an error in XML document (2, 2)."}
More info on the exception reveals the “Unexpected AnotherDataType xmlns:..”. But everything was fine minutes ago! No it wasn’t – the exception was always thrown, but it’s been caught and ignored. Another thing left my cookie in an illegal state which was enough to blank my screen, and I’ve accused the XML exception. Shame on me!
There is an excellent article and valuable comments about XML deserialization here: http://tech.pro/tutorial/798/csharp-tutorial-xml-serialization