XML Validation using XSD File

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Adhal

    XML Validation using XSD File

    Hello,

    I have an XML and XSD file. When I do validation in the XML file I only get the first error. Is
    it possible to list all errors?

    One other question.

    -------------Microsoft Sample Code -------------------
    XmlReaderSettin gs settings = new XmlReaderSettin gs();
    settings.Schema s.Add("http://www.contoso.com/books", "contosoBooks.x sd");
    settings.Valida tionType = ValidationType. Schema;

    XmlReader reader = XmlReader.Creat e("contosoBooks .xml", settings);
    XmlDocument document = new XmlDocument();
    document.Load(r eader);
    ValidationEvent Handler eventHandler = new ValidationEvent Handler(Validat ionEventHandler );
    document.Valida te(eventHandler );
    -----------------http://msdn.microsoft. com/en-us/library/ms162371.aspx ---------------


    What is the point of first creating a XmlReader and passing in the XmlReaderSettin gs ?

    Wouldn't it be easier to just use something like this:

    ----------------------------------------------
    XmlDocument document = new XmlDocument();
    document.Load(F ilename)

    XmlReaderSettin gs settings = new XmlReaderSettin gs();
    settings.Valida tionType = ValidationType. Schema;

    document.Schema s.Add(settings. Schemas.Add(nul l, "contosoBooks.x ml"));
    ValidationEvent Handler eventHandler = new ValidationEvent Handler(Validat ionEventHandler );
    document.Valida te(eventHandler );
    ----------------------------------------------

    Am I missing something? What is the point of loading in the reader first.

    Thank you for taking the time to read this.


    --
    Adhal Freeware


    There are only 10 types of people in the world: Those who understand binary, and those who don't.
  • Martin Honnen

    #2
    Re: XML Validation using XSD File

    Adhal wrote:
    I have an XML and XSD file. When I do validation in the XML file I
    only get the first error. Is it possible to list all errors?
    If you set up an ValidationEvent Handler then the validation process
    should report all errors it finds. If you do not set up an
    ValidationEvent Handler then the fist validation error throws an exception.
    If you do not get "all errors" reported then we need to look at the
    details of the XML and the schema(s).
    One other question.
    >
    -------------Microsoft Sample Code -------------------
    XmlReaderSettin gs settings = new XmlReaderSettin gs();
    settings.Schema s.Add("http://www.contoso.com/books",
    "contosoBooks.x sd");
    settings.Valida tionType = ValidationType. Schema;
    >
    XmlReader reader = XmlReader.Creat e("contosoBooks .xml", settings);
    XmlDocument document = new XmlDocument();
    document.Load(r eader);
    ValidationEvent Handler eventHandler = new
    ValidationEvent Handler(Validat ionEventHandler );
    document.Valida te(eventHandler );
    -----------------http://msdn.microsoft. com/en-us/library/ms162371.aspx
    ---------------
    >
    >
    What is the point of first creating a XmlReader and passing in the
    XmlReaderSettin gs ?
    I would rather ask what is the point of using the Validate method? The
    Validate method is there to validate an in-memory DOM document after you
    have performed changes with the DOM API (e.g. create and inserted nodes
    or removed nodes). If you don't do that and have the XML file and the
    XSD schema file(s) then you do not need to use an XmlDocument at all but
    you can simply use an XmlReader with the proper XmlReaderSettin gs as in
    the following approach:

    XmlReaderSettin gs settings = new XmlReaderSettin gs();
    settings.Valida tionType = ValidationType. Schema;
    settings.Schema s.Add(null, "schema1.xs d");
    // add further schemas if needed e.g.
    settings.Schema s.Add(null, "schema2.xs d");
    settings.Valida tionEventHandle r += new
    ValidationEvent Handler(yourHan dler);

    using (XmlReader reader = XmlReader.Creat e("file.xml", settings))
    {
    while (reader.Read()) {}
    }




    --

    Martin Honnen --- MVP XML

    Comment

    • Mr. Arnold

      #3
      Re: XML Validation using XSD File


      "Adhal" <user@example.n etwrote in message
      news:%235FWN%23 D2IHA.4572@TK2M SFTNGP03.phx.gb l...
      Hello,
      >
      I have an XML and XSD file. When I do validation in the XML file I only
      get the first error. Is it possible to list all errors?
      >
      The code in the link using the loop and dumping errors to an array is what I
      used.



      Comment

      • Martin Honnen

        #4
        Re: XML Validation using XSD File

        Adhal wrote:
        I used XmlDocument to get the first element (DocumentElemen t) then I
        loaded the correct schema to
        validate it against.
        >
        Is there a better way to do this?
        >
        I can use reader to parse through the first few elements, I guess.
        I tend to use XmlDocument when I want to manipulate an existing XML
        document. If needed/wanted I can then also validate the changes with the
        Validate method.
        If I purely want to validate an existing document then I use XmlReader.
        Even if I needed to look at the root/document element first to decide
        which schema to use I would use XmlReader, one which reads the root
        element to decide on the schema, a second to validate against the
        schema. That should still be faster and consume much less memory than
        using an XmlDocument.


        --

        Martin Honnen --- MVP XML

        Comment

        Working...