Validating XML

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

    Validating XML

    I have to validate xml file against xsd file using c#.
    XmlReader only check for the well formated doucment but i want to
    check for every thing like space,datatype and any extra text in xml
    file.

    pls help me do this

    tell me some library if there is any to validate xml document.
  • Martin Honnen

    #2
    Re: Validating XML

    Mahain wrote:
    I have to validate xml file against xsd file using c#.
    XmlReader only check for the well formated doucment but i want to
    check for every thing like space,datatype and any extra text in xml
    file.
    Use an XmlReader with XmlReaderSettin gs set up for validation:

    XmlReaderSettin gs settings = new XmlReaderSettin gs();
    settings.Valida tionType = ValidationType. Schema;
    settings.Schema s.Add(null, "schema.xsd ");
    bool valid = true;
    settings.Valida tionEventHandle r += delegate(object sender,
    ValidationEvent Args vargs)
    {
    if (vargs.Severity == XmlSeverityType .Error)
    {
    valid = false;
    }
    Console.WriteLi ne("{0}: {1}", vargs.Severity, vargs.Message);
    };

    using (XmlReader reader = XmlReader.Creat e(@"doc.xml", settings))
    {
    while (reader.Read()) {}
    }
    Console.WriteLi ne("Document is {0}.", valid ? "valid": "not valid");

    See also the MSDN section:
    <URL:http://msdn.microsoft. com/en-us/library/hdf992b8(VS.80) .aspx>

    --

    Martin Honnen --- MVP XML

    Comment

    • Peter Morris

      #3
      Re: Validating XML

      I have a new job. Well, I say "new" but I actually started in December 2005. Anyway, this "new" job requires me to develop compact framewo...



      Comment

      • Mahain

        #4
        Re: Validating XML

        On May 8, 3:55 pm, Martin Honnen <mahotr...@yaho o.dewrote:
        Mahain wrote:
        I have to validate xml file against xsd file using c#.
        XmlReader only check for the well formated doucment but i want to
        check for every thing like space,datatype and any extra text in xml
        file.
        >
        Use an XmlReader with XmlReaderSettin gs set up for validation:
        >
        XmlReaderSettin gs settings = new XmlReaderSettin gs();
        settings.Valida tionType = ValidationType. Schema;
        settings.Schema s.Add(null, "schema.xsd ");
        bool valid = true;
        settings.Valida tionEventHandle r += delegate(object sender,
        ValidationEvent Args vargs)
        {
        if (vargs.Severity == XmlSeverityType .Error)
        {
        valid = false;
        }
        Console.WriteLi ne("{0}: {1}", vargs.Severity, vargs.Message);
        };
        >
        using (XmlReader reader = XmlReader.Creat e(@"doc.xml", settings))
        {
        while (reader.Read()) {}
        }
        Console.WriteLi ne("Document is {0}.", valid ? "valid": "not valid");
        >
        See also the MSDN section:
        <URL:http://msdn.microsoft. com/en-us/library/hdf992b8(VS.80) .aspx>
        >
        --
        >
        Martin Honnen --- MVP XML
        http://JavaScript.FAQTs.com/
        same result as XMLReader

        Comment

        Working...