views:

275

answers:

2

I am using XML serialization to read a large user input file. If there is a semantic error in the input that is not discovered during reading, I'd like to be able to tell the user where (file, line, column) they can find the offending element. Is this possible with XML serialization?

CLARIFICATION: The code in question is an IoC container for Compact Framework; many of the possible semantic errors relate to things like reflection failures while initializing the final object graph. These are effectively run-time errors, not read-time or compile-time errors. However, I'd like to tell them not only that a property set failed because class Blah doesn't have property Fred, but that the property set in question is on line 1337 of Objects.xml.

  • The errors do not result in deserialization exceptions. The input is valid for deserialization purposes and contains no syntax or data type errors.
  • The errors cannot be detected by enabling schema validation. The input is schematically valid.
  • The errors cannot be detected during the read/deserialization process because the types of target objects may not yet have been resolved.
A: 

This MSDN article on troubleshooting XML serialization might help -- http://msdn.microsoft.com/en-us/library/aa302290.aspx It indicates that:

Just like in the case of serialization, the Deserialize() method throws an InvalidOperation exception with the Message

There is an error in XML document
(<line>, <column>).

whenever a problem occurs. This exception typically contains the real exception in the InnerException property. The type of the InnerException varies according to the actual error that occurred while reading the XML document.

Clarification -- if you can cause the object to self validate and fail during deserializaton, this would be the best approach to getting the problem location at the time when you have all the context.

Steve Gilham
The error cannot be detected during the deserialization, the data must be saved in the final object graph for later run-time error reporting.
Jeffrey Hantin
A: 

If you have the schemas that describe the document, then you can use an XmlReader with validation when you deserialize. Handle the validation errors and you'll be able to give your users perhaps more detail than they would like.

Note that XML Schema validation errors are not very user-friendly. You might want to work in the opposite direction: make it harder for your users to input invalid documents. Using a tool like InfoPath that puts a forms UI over XML instance documents would be one way to do that.

Another way would be to use the XML editor in Visual Studio Express 2008. If you provide it with the schemas, it will not only validate the XML, it will also provide IntelliSense - it will show the valid next elements or attributes, and will even show tooltips containing documentation from the schema.


There will be no way to do what you're looking for. Many of these semantic failures you're referring to will show up as exceptions somewhere deep in the serialization process. The first of these exceptions will terminate the process. You will only be able to catch such an exception outside of your call to XmlSerializer.DeSerialize. At that point, any context is gone, and all you have is the chain of Exception.InnerException.InnerException...

If you do your own deserialization by implementing IXmlSerializable, then you'll be able to keep the exception handling inside the class, with access to the XmlReader being used. If that reader maintains line and column information, then you should be able to add that information to the exception.

John Saunders
Please see my clarification. The errors are detected very late, and cannot possibly be detected at edit-time in a user interface, or at read-time with any sort of validation logic.
Jeffrey Hantin