Validation XML against XSD

Last post 07-07-2009 10:26 AM by Martin_Honnen. 2 replies.

Sort Posts:

  • Validation XML against XSD

    07-06-2009, 11:45 PM
    • Member
      5 point Member
    • alangrutter
    • Member since 03-07-2007, 1:39 AM
    • Wellington, New Zealand
    • Posts 39

    Hi,

    I have created an XSD file which I want to use to validate XML files against. I am using the following code to do the validation

                    using (XmlReader reader = XmlReader.Create(fileUpload.PostedFile.InputStream)) {
                        XmlSchemaSet sc = new XmlSchemaSet();
                        sc.Add("", Request.PhysicalApplicationPath + "UploadRfid.xsd");
    
                        try {
                            XDocument doc = XDocument.Load(reader);
                            doc.Validate(sc, new ValidationEventHandler(ValidationCallback));
                      }
                        catch (XmlException exc) {
                        }
                        finally {
                        }
                    }
    

    If I test this using an XML file with no namespaces or XSD declarations, it works fine and gives expected result (i.e. validates againast my schema).

    However when I test this with a different XML file (which is nothing like my schema) that has inline XSD specified (not my one), the validation succeeds and does not get any errors even though the XML file is nothing like my schema.

    My schema document begins with

    <?xml version="1.0" encoding="utf-8"?>
    <xs:schema id="myschema" xmlns:xs="http://www.w3.org/2001/XMLSchema">

    How can I get the second XML to fail my schema validation?



    Regards
    Alan
    Filed under:
  • Re: Validation XML against XSD

    07-07-2009, 2:02 AM

    that has inline XSD specified (not my one), the validation suc

    Can u try removing this inline schema declaration? i think it gives preference to it...


    Please mark this post as Answer if it is of help to you!

    " Every wall is a door..! "
  • Re: Validation XML against XSD

    07-07-2009, 10:26 AM
    Answer

     Schema based validation has some oddities, one of them being that the validator looks at the root element of the XML input document and its namespace to look for a schema with that targetNamespace. If it does not find such a schema then so called lax validation is performed. Thus if you have a certain schema as you have shown that does not have a targetNamespace and you try to validate an XML document with elements in a certain namespace then that schema does not apply and lax validation is performed. So you either need to write your own code that compares doc.Root.Name.NamespaceName against the target namespace of your schema(s) or you need to use an XmlReader for validation where you can use XmlReaderSettings with a flag to report validation warnings as in that case the reader will emit a warning to your validation event handler any time it finds an element in a namespace for which there is no matching schema. See also this blog entry that recommends to turn on warnings.

    Martin Honnen --- MVP XML
    My blog
Page 1 of 1 (3 items)