I am trying to test Xsd Validation using Saxon. When I get to the actual validation, only the first error is caught because validator.Run() throws an exception when it gets to the first error, and does not continue afterwards. This is obviously not what you want when you have an xml file with many errors. Is there a way to continue validation after the exception is thrown or is there another method of validation using Saxon?
This code is based off the one example for validation that Saxon has in it’s samples folder of documentation and this is the section that runs the validation.
SchemaValidator validator = manager.NewSchemaValidator();
using (Stream xmlFile = File.OpenRead(fileName))
{
using (XmlReader xmlValidatingReader = XmlReader.Create(xmlFile))
{
validator.SetSource(xmlValidatingReader);
validator.ErrorList = new ArrayList();
try
{
validator.Run();
}
catch (Exception)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("Instance validation failed with " + validator.ErrorList.Count + " errors");
foreach (StaticError error in validator.ErrorList)
{
sb.AppendLine("At line " + error.LineNumber + ": " + error.Message);
tbXsdOutput.Text = sb.ToString();
}
return;
}
}
}
Here’s how I configured Saxonica to return more than one error:
Working code below:
You can see more about the VALIDATION_WARNINGS option here.