1

I have read that XmlDocument.Validate method does not catch multiple errors:

From MSDN: here

If schema validation errors occur during validation the XmlDocument becomes partially validated with some nodes with correct type information and some without.

From StackoverFlow: here

That's exactly the expected behavior of XmlDocument.Validate method. Once it finds a validation error it stops validate process and returns the error. So, the user has to fix that error and validate again.

This behavior is different from the Visual studio error list. For example, if you have a single syntax error in the code sometimes it returns 100s of errors. But actually you have to fix only one at one place. So, there can be both pros and cons depends on the circumstance. However, I don't think you could easily get all the validation errors for a XMLDocument, it works in a different way inherently.

But is there any possible someone know about which can catch all the errors of the validation?

Community
  • 1
  • 1
Coding man
  • 957
  • 1
  • 18
  • 44
  • 1
    Once an error is found, the rest of the document is inconsistent so it can't be validated. I'm sure there are ways but they must not be trivial (and definitely, you can't with `XmlDocument.Validate`) – Jcl Mar 11 '16 at 07:50

1 Answers1

1

It appears that the XML Serialisation NuGet package supports the type of validation you are looking for.

In particular, its IsValidXml method utilizes an XmlReader (which is a .NET type), to enable the collating of multiple errors (using an out parameter).

Here is the implementation (from the Github source)

/// <summary>
/// Validates an XML file against a given XSD schema with validation error messages.
/// </summary>
/// <param name="xmlFileUrl">File location of the XML file to validate.</param>
/// <param name="xmlSchemaFile">File location of the XSD schema to validate the XML file against.</param>
/// <param name="validationErrors">Collection of validation error information objects if the XML file violates the XSD schema.</param>
/// <returns>True if a valid XML file according to the XSD schema, false otherwise.</returns>
public static bool IsValidXml(
    string xmlFileUrl,
    string xmlSchemaFile,
    out IList<Tuple<object, XmlSchemaException>> validationErrors)
{
    var internalValidationErrors = new List<Tuple<object, XmlSchemaException>>();
    var readerSettings = XmlSchemaReader(
        xmlSchemaFile,
        (obj, eventArgs) => internalValidationErrors.Add(
            new Tuple<object, XmlSchemaException>(obj, eventArgs.Exception))
    );

    using (var xmlReader = new XmlTextReader(xmlFileUrl))
    using (var objXmlReader = XmlReader.Create(xmlReader, readerSettings))
    {
        try
        {
            while (objXmlReader.Read()) { }
        }
        catch (XmlSchemaException exception)
        {
            internalValidationErrors.Add(
                new Tuple<object, XmlSchemaException>(objXmlReader, exception));
        }
    }

    validationErrors = internalValidationErrors;

    return !validationErrors.Any();
}
devuxer
  • 41,681
  • 47
  • 180
  • 292