S
S
Samir Kurbanov2019-04-17 15:50:18
C++ / C#
Samir Kurbanov, 2019-04-17 15:50:18

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);
    }

I need the xml validation to continue after finding the first error in order to eventually collect information about the position , the type of all errors found in the xml file.
After studying the microsoft documentation, it seems to me that further validation of the xml file after the exception is thrown is not possible, but still I will write my question here to make sure exactly if this is the case)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
F
Farwisdomer, 2019-04-17
@kurbanov_samir

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.

D
d-stream, 2019-04-17
@d-stream

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.

S
sergey, 2019-04-17
kuzmin @sergueik

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 question

Ask a Question

731 491 924 answers to any question