I'm trying to validate an Xml fragment using an Xml Schema with the XDocument.Validate extension method. Whenever an invalid Xml fragment is used the ValidationEventHandler fires properly, however both the LineNumber and LinePosition properties of the XmlSchemaValidationException 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 for informing a user as to why the validation failed. When this method is used, however, the LineNumber and LinePosition are both 0.
The snippet seems simple enough and appears to work as expected in terms of validating against both valid and invalid Xml fragments.
Thanks in advance!