I am trying to test an Xml fragment using an Xml schema using the XDocument.Validate extension method. Whenever an invalid Xml fragment is used, the ValidationEventHandler fires correctly, however both the LineNumber and LinePosition properties of the XmlSchemaValidationEx exception are 0.
private bool Validate(XDocument doc)
{
bool isValid = true;
List<string> validationErrors = new List<string>();
XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add(null, "MyCustomSchema.xsd");
doc.Validate(schemas, (sender, args) =>
{
validationErrors.Add(String.Format("{0}: {1} [Ln {2} Col {3}]",
args.Severity,
args.Exception.Message,
args.Exception.LineNumber,
args.Exception.LinePosition));
isValid = false;
}, false);
return isValid;
}
My goal in the above example is to use validationErrors to inform the user why the validation failed. However, when this method is used, LineNumber and LinePosition are 0.
The snippet looks fairly simple and seems to work as expected in terms of validating both valid and invalid Xml snippets.
Thanks in advance!