I havve created a C# class representing an XSD using the XSD.exe tool. I use a valdiation code to check the consistency of the XML with the XSD. I got this working, but using a
Line and character numbers are reported to the exception message via the IXmlLineInfo interface, which XmlTextReader implements. Of note, both the line number and line position start with number one (and are zero only when not available).
Thus "(235, 17)"
are the line number and position in the line (starting at one) at which the reader was positioned when the error was thrown. In most cases, that will be actual position in the XML file at which the error occurs. However, sometimes the XML Serializer will have advanced the reader to the next node before the error is thrown. From experimentation:
If the XML is not well-formed, then the error will be where the line and position report.
If the XML is well formed but an attribute value cannot be deserialized, then the attribute with the bad value will be where the line and position report.
However, if the XML is well-formed and an element value cannot be deserialized, the XML reader will already have been advanced past the end of the element to the beginning of the next XML reader node (typically an element opening or closing), so the element causing the problem will be one one immediately before the reported location, possibly on the previous line.
If the error is reported at (0, 0)
then the reader was unable to begin parsing the XML. Likely problems include:
string
using StringReader
and there is a BOM embedded at the beginning. See for instance XmlReader breaks on UTF-8 BOM for details.