Answer the question
In order to leave comments, you need to log in
How to detect ALL errors and inconsistencies in the xsd schema of an xml file?
C# has an XmlSchemaSet class that can be used to work with xml files.
The keyword of the question is all .
The fact is that the code from the documentation of the microsoft site, when checking the xml file according to the xsd schema, throws an exception (if there are errors in the xml file) and the program stops when the first error is found.
C# code:
public static void Main()
{
// Create the XmlSchemaSet class.
XmlSchemaSet sc = new XmlSchemaSet();
// Add the schema to the collection.
sc.Add(null, "shema.xsd");
// Set the validation settings.
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.Schemas = sc;
settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
// Create the XmlReader object.
XmlReader reader = XmlReader.Create("my.xml", settings);
// Parse the file.
while (reader.Read()) ;
}
// Display any validation errors.
private static void ValidationCallBack(object sender, ValidationEventArgs e)
{
Console.WriteLine("Validation Error: {0}", e.Message);
}
Answer the question
In order to leave comments, you need to log in
https://social.msdn.microsoft.com/Forums/en-US/e64...
In my opinion, it's better to first check the xml for validity (correct structure) and only then according to the xsd schema.
I need the xml validation to continue after finding the first error in order to eventually collect information about the position, type of all errors found in the xml file.In fact, after the first mistake, looking further is practically pointless, since all other errors can be the result of the first one.
take any of the ones on github, for example https://github.com/andreburgaud/xvalidatr and change it to continue after Exception,
I somehow did exactly the same with the yaml validator https://github.com/aaubry/YamlDotNet . so that one run reveals more than one error (bad commits happen often in yaml
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question