Albert Lyubarsky
Albert Lyubarsky

Reputation: 450

XML XSD validation. When it occurs

I cannot understand when validation of XML occurs on Load or on Validate. Here is following code...

        XmlDocument doc = null;
        try
        {

            XmlReaderSettings settings = new XmlReaderSettings( );
            settings.Schemas.Add("http://xxx/customs/DealFile/Common/ReleaseGoodsMessage",
                                 ConfigurationManager.AppSettings.Get("Schemas"));
            settings.ValidationType = ValidationType.Schema;


            using (XmlReader reader = XmlReader.Create(path, settings)) {
                doc = new XmlDocument( );
                doc.Load(reader);
            }                

            ValidationEventHandler eventHandler = new ValidationEventHandler(ValidationEventHandler);

            doc.Validate(eventHandler);

        }
        catch(XmlSchemaException xmlErr)
        {
               // Do something
        }

I expect a validation to occur on line doc.Validate(eventHandler); However it always occurs on doc.Load(reader); . I've got an exception if something wrong with XML.

        XMLHelpers.LoadXML(@"C:\work\Xml2Db\Xml2Db\Data\Tests\BadData\01.xml")
    Exception thrown: 'System.Xml.Schema.XmlSchemaValidationException' in System.Xml.dll
    xmlErr.Message
    "The 'http://xxx/customs/DealFile/Common/ReleaseGoodsMessage:governmentProcedureType' element is invalid - 
The value 'a' is invalid according to its datatype 'Int' - The string 'a' is not a valid Int32 value."

And this is the code from Microsoft's example https://learn.microsoft.com/en-us/dotnet/api/system.xml.xmldocument.validate?view=netcore-3.1

       try
    {
        XmlReaderSettings settings = new XmlReaderSettings();
        settings.Schemas.Add("http://www.contoso.com/books", "contosoBooks.xsd");
        settings.ValidationType = ValidationType.Schema;

        XmlReader reader = XmlReader.Create("contosoBooks.xml", settings);
        XmlDocument document = new XmlDocument();
        document.Load(reader);

        ValidationEventHandler eventHandler = new ValidationEventHandler(ValidationEventHandler);

        // the following call to Validate succeeds.
        document.Validate(eventHandler);
        ...

It's actually the same. But, pay attention on comment // the following call to Validate succeeds. . They also expect to get validation on the line document.Validate(eventHandler);

What's going on.

Upvotes: 0

Views: 380

Answers (2)

Richard
Richard

Reputation: 613

As your block of code sets up the settings object, it sets a schema and the Validator to use ValidationType.Schema (i.e.: use the schema).

When you setup the XmlReader, using your settings it's setup to validate according to the schema, too - which is causing your schema-based error/exception.

The call to document.Validate(eventHandler); is completely redundant, because it will succeed in all circumstances - because the xml has already been validated. The comment is correct "the following call to Validate succeeds" because the document has already been proved valid.

Upvotes: 1

kimbert
kimbert

Reputation: 2422

I suspect that you are failing to distinguish between XML that is well-formed and XML that is valid.

A well-formed XML document satisfies all of the rules of the XML specification. If it does not, you should get a well-formedness error from any XML parser.

If you also choose to

a) supply an XSD that describes you XML document and

b) tell your XML processor to validate against that XSD

then the XML processor will also check that the document satisfies the rules in the XML schema (an XML Schema is composed of one or more XSDs).

If you are still not sure, edit your question and supply the error message(s) that you are seeing. You don't need to include any confidential information - the error template is enough to tell which kind of error it is.

Upvotes: 0

Related Questions