XDocument.Validate custom validation messages
I'm validating an XML file against a schema using XDocument.Validate and get validation messages which look something like this:
The 'http://www.blablah.co.uk/schemas/cbds:DOB' element is invalid - The value '1999-0dsf2-21' is invalid according to its datatype 'http://www.w3.org/2001/XMLSchema:date' - The string '1999-0dsf2-21' is not a valid XsdDateTime value.
This doesn't look too user friendly and I was wondering if it's possible to customize this message in any way? It'd be nice if I could provide a tokenized string like:
The value {0} is invalid for th开发者_C百科e element {1}
This is not a complete solution, but this might help you get to where you want to be.
I believe that method can take a ValidationEventHandler Delegate. In your delegate, you can access the XML node if the exception is a XmlSchemaValidationException. If it is, you can use the XmlSchemaValidationException.SourceObject property to retrieve the invalid xml node.
精彩评论