How to get the XPath (or Node) for the location of an XML schema validation failure?
Sender of validation event is a source of event. So, you can search over the network for code which gets XPath for node (e.g. Generating an XPath expression) and generate XPath for source of event:
doc.Validate(schemas, (sender, args) => {
if (sender is XObject)
{
xpath = ((XObject)sender).GetXPath();
}
});
Take it :-)
var xpath = new Stack<string>();
var settings = new XmlReaderSettings
{
ValidationType = ValidationType.Schema,
ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings,
};
MyXmlValidationError error = null;
settings.ValidationEventHandler += (sender, args) => error = ValidationCallback(sender, args);
foreach (var schema in schemas)
{
settings.Schemas.Add(schema);
}
using (var reader = XmlReader.Create(xmlDocumentStream, settings))
{
// validation
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
xpath.Push(reader.Name);
}
if (error != null)
{
// set "failing XPath"
error.XPath = xpath.Reverse().Aggregate(string.Empty, (x, y) => x + "/" + y);
// your error with XPath now
error = null;
}
if (reader.NodeType == XmlNodeType.EndElement ||
(reader.NodeType == XmlNodeType.Element && reader.IsEmptyElement))
{
xpath.Pop();
}
}
}