I use XDocument.Validate (it seems to work the same as XmlDocument.Validate) to validate an XML document on XSD - this works well and I am informed of validation errors.
However, only some information appears to be [reliable] displayed in the ValidationEventHandler (and XmlSchemaException), for example:
- error message (ie, the "X" attribute is invalid - the value "Y" is invalid according to its data type "Z" - template constraint failure "),
- heaviness
I would like to get a “failing XPath” for a validation failure (where this makes sense): that is, I would like to receive a failure in relation to an XML document (as opposed to XML text).
Is there a way to get XPath failure information from XDocument.Validate ? If not, is it possible to get "failing XPath" using another XML validation method such as XmlValidatingReader 1 ?
Background:
XML will be sent as data to my web service with automatic conversion (via JSON.NET) from JSON to XML. Because of this, I am starting to process XDocument 1 data and not text that does not have a guaranteed order due to the original JSON data. For reasons that I don’t need, the REST client is basically a wrapper for HTML form fields over an XML document, and the server validation takes place in two parts: validation of the XML schema and validation of business rules.
In confirming the business rule, it is easy to return “XPath” for fields that do not meet the requirements, which can be used to indicate a field failure (s) on the client. I would like to extend this to validating the XSD schema, which takes care of validating the underlying structure and, more importantly, the underlying “data type” and “existence” of the attributes. However, due to the desired automatic process (i.e., highlighting the corresponding failure field) and source conversion, the raw text message and the source row / column numbers are not very useful in themselves.
Here is a snippet of verification code:
// Start with an XDocument object - created from JSON.NET conversion XDocument doc = GetDocumentFromWebServiceRequest(); // Load XSD var reader = new StringReader(EmbeddedResourceAccess.ReadResource(xsdName)); var xsd = XmlReader.Create(reader, new XmlReaderSettings()); var schemas = new XmlSchemaSet(); schemas.Add("", xsd); // Validate doc.Validate(schemas, (sender, args) => { // Process validation (not parsing!) error, // but how to get the "failing XPath"? });
Update: I discovered Capture Schema Information while validating an XDocument that references "Accessing the XML Schema Information while Validating a Document" ( cached ), from which I determined two things:
An XmlSchemaException can be specialized in an XmlSchemaValidationException that has a SourceObject property, however this always returns null during validation: When an XmlSchemaValidationException is thrown when the validator checks the XmlReader, the value of the SourceObject property is null.
I can read the document (via XmlReader.Read ) and "remember" the path to the validation callback. Although this “looks like it works” in the initial tests (without the ValidationCallback), it feels completely inelegant for me, but I still have little to find.