Validating an XML against referenced XSD in C#
Asked Answered
G

5

174

I have an XML file with a specified schema location such as this:

xsi:schemaLocation="someurl ..\localSchemaPath.xsd"

I want to validate in C#. Visual Studio, when I open the file, validates it against the schema and lists errors perfectly. Somehow, though, I can't seem to validate it automatically in C# without specifying the schema to validate against like so:

XmlDocument asset = new XmlDocument();

XmlTextReader schemaReader = new XmlTextReader("relativeSchemaPath");
XmlSchema schema = XmlSchema.Read(schemaReader, SchemaValidationHandler);

asset.Schemas.Add(schema);

asset.Load(filename);
asset.Validate(DocumentValidationHandler);

Shouldn't I be able to validate with the schema specified in the XML file automatically ? What am I missing ?

Georgettageorgette answered 15/4, 2009 at 12:40 Comment(1)
Refer the MSDN sample : msdn.microsoft.com/en-us/library/…Ide
N
179

You need to create an XmlReaderSettings instance and pass that to your XmlReader when you create it. Then you can subscribe to the ValidationEventHandler in the settings to receive validation errors. Your code will end up looking like this:

using System.Xml;
using System.Xml.Schema;
using System.IO;

public class ValidXSD
{
    public static void Main()
    {

        // Set the validation settings.
        XmlReaderSettings settings = new XmlReaderSettings();
        settings.ValidationType = ValidationType.Schema;
        settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema;
        settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation;
        settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
        settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);

        // Create the XmlReader object.
        XmlReader reader = XmlReader.Create("inlineSchema.xml", settings);

        // Parse the file. 
        while (reader.Read()) ;

    }
    // Display any warnings or errors.
    private static void ValidationCallBack(object sender, ValidationEventArgs args)
    {
        if (args.Severity == XmlSeverityType.Warning)
            Console.WriteLine("\tWarning: Matching schema not found.  No validation occurred." + args.Message);
        else
            Console.WriteLine("\tValidation error: " + args.Message);

    }
}
Nitrogenous answered 15/4, 2009 at 17:16 Comment(8)
+1 although should update to use using clause for completeness :)Ferdinana
If you're looking to compare against an XSD file add the following line to the above code: settings.Schemas.Add("YourDomainHere", "yourXSDFile.xsd");Houphouetboigny
To get Line# and position# of the error simply use: args.Exception.LineNumber... in ValidationCallBackPriestess
What if the schema i have doesn't have a namespace?Exhilarant
What's means inlineSchema for the xml file ? And if I have xml file with xmlns:xsi, xmlns and xsi:schemaLocation, and I have xsd file with targetNamespace, xmlns, and xmlns:xs ? full filesRoseannaroseanne
Using lambda, better IMHO, more clarity code settings.ValidationEventHandler += (o, args) => { errors = true; // More code };Roseannaroseanne
you are looking for ProcessInlineSchema. Defined in XML itself. Above solution is correct for it.Goldarn
When compare against an XSD file, you can use : settings.Schemas.Add("xml file schema namespace", "path to xsd file"); If the there is no "xml file schema namespace", you can use : settings.Schemas.Add(null, "path to xsd file");Calcicole
S
123

A simpler way, if you are using .NET 3.5, is to use XDocument and XmlSchemaSet validation.

XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add(schemaNamespace, schemaFileName);

XDocument doc = XDocument.Load(filename);
string msg = "";
doc.Validate(schemas, (o, e) => {
    msg += e.Message + Environment.NewLine;
});
Console.WriteLine(msg == "" ? "Document is valid" : "Document invalid: " + msg);

See the MSDN documentation for more assistance.

Stillborn answered 3/8, 2012 at 18:47 Comment(5)
That method requires you know the schema beforehand rather than taking the inline schema from the xml.Counterfeit
this works fine but throws error when xml document contains some html tag like <catalog>my <i> new </i> catalog....</catalog> in above case html tags like "<i>" creates an issue as it is the value of "<catalog>" ... how to validate itWarmongering
@AnilPurswani: If you wish to put HTML into an XML document, you need to wrap it in CDATA. <catalog><![CDATA[my <i> new </i> catalog....]]></catalog> is the correct way to do that.Ferro
Simple and elegant! This works very well when validating against a fixed schema set (which is our case, and a big one with multiple folders and files). I'm already thinking about caching the XmlSchemaSet to be reused between calls to the Validator. Thanks a lot!Skean
It does not work when the default namespace in XSD is different than in XML.Hudnall
H
24

personally I favor validating without a callback:

public bool ValidateSchema(string xmlPath, string xsdPath)
{
    XmlDocument xml = new XmlDocument();
    xml.Load(xmlPath);

    xml.Schemas.Add(null, xsdPath);

    try
    {
        xml.Validate(null);
    }
    catch (XmlSchemaValidationException)
    {
        return false;
    }
    return true;
}

(see Timiz0r's post in Synchronous XML Schema Validation? .NET 3.5)

Huckaby answered 6/11, 2015 at 10:24 Comment(2)
The callback provides you with some extra information about which line in your xml is not correct. This method is very binary, either right or wrong :)Huckaby
you can get the exception message :)Simonne
C
22

The following example validates an XML file and generates the appropriate error or warning.

using System;
using System.IO;
using System.Xml;
using System.Xml.Schema;

public class Sample
{

    public static void Main()
    {
        //Load the XmlSchemaSet.
        XmlSchemaSet schemaSet = new XmlSchemaSet();
        schemaSet.Add("urn:bookstore-schema", "books.xsd");

        //Validate the file using the schema stored in the schema set.
        //Any elements belonging to the namespace "urn:cd-schema" generate
        //a warning because there is no schema matching that namespace.
        Validate("store.xml", schemaSet);
        Console.ReadLine();
    }

    private static void Validate(String filename, XmlSchemaSet schemaSet)
    {
        Console.WriteLine();
        Console.WriteLine("\r\nValidating XML file {0}...", filename.ToString());

        XmlSchema compiledSchema = null;

        foreach (XmlSchema schema in schemaSet.Schemas())
        {
            compiledSchema = schema;
        }

        XmlReaderSettings settings = new XmlReaderSettings();
        settings.Schemas.Add(compiledSchema);
        settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
        settings.ValidationType = ValidationType.Schema;

        //Create the schema validating reader.
        XmlReader vreader = XmlReader.Create(filename, settings);

        while (vreader.Read()) { }

        //Close the reader.
        vreader.Close();
    }

    //Display any warnings or errors.
    private static void ValidationCallBack(object sender, ValidationEventArgs args)
    {
        if (args.Severity == XmlSeverityType.Warning)
            Console.WriteLine("\tWarning: Matching schema not found.  No validation occurred." + args.Message);
        else
            Console.WriteLine("\tValidation error: " + args.Message);

    }
}

The preceding example uses the following input files.

<?xml version='1.0'?>
<bookstore xmlns="urn:bookstore-schema" xmlns:cd="urn:cd-schema">
  <book genre="novel">
    <title>The Confidence Man</title>
    <price>11.99</price>
  </book>
  <cd:cd>
    <title>Americana</title>
    <cd:artist>Offspring</cd:artist>
    <price>16.95</price>
  </cd:cd>
</bookstore>

books.xsd

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns="urn:bookstore-schema"
    elementFormDefault="qualified"
    targetNamespace="urn:bookstore-schema">

 <xsd:element name="bookstore" type="bookstoreType"/>

 <xsd:complexType name="bookstoreType">
  <xsd:sequence maxOccurs="unbounded">
   <xsd:element name="book"  type="bookType"/>
  </xsd:sequence>
 </xsd:complexType>

 <xsd:complexType name="bookType">
  <xsd:sequence>
   <xsd:element name="title" type="xsd:string"/>
   <xsd:element name="author" type="authorName"/>
   <xsd:element name="price"  type="xsd:decimal"/>
  </xsd:sequence>
  <xsd:attribute name="genre" type="xsd:string"/>
 </xsd:complexType>

 <xsd:complexType name="authorName">
  <xsd:sequence>
   <xsd:element name="first-name"  type="xsd:string"/>
   <xsd:element name="last-name" type="xsd:string"/>
  </xsd:sequence>
 </xsd:complexType>

</xsd:schema>
Churchless answered 3/8, 2012 at 14:57 Comment(0)
A
13

I had do this kind of automatic validation in VB and this is how I did it (converted to C#):

XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.ValidationFlags = settings.ValidationFlags |
                           Schema.XmlSchemaValidationFlags.ProcessSchemaLocation;
XmlReader XMLvalidator = XmlReader.Create(reader, settings);

Then I subscribed to the settings.ValidationEventHandler event while reading the file.

Anglophile answered 15/4, 2009 at 12:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.