XmlValidatingReader.ValidationEventHandler Event


Sets an event handler for receiving information about document type definition (DTD), XML-Data Reduced (XDR) schema, and XML Schema definition language (XSD) schema validation errors.

Namespace:   System.Xml
Assembly:  System.Xml (in System.Xml.dll)

public event ValidationEventHandler ValidationEventHandler


The XmlValidatingReader class is obsolete in .NET Framework 2.0. You can create a validating XmlReader instance by using the XmlReaderSettings class and the Create method. For more information, see the Remarks section of the XmlReader reference page.

These events occur during Read and only if a ValidationType of DTD, XDR, Schema, or Auto is specified.

If no event handler is provided, an XmlException is thrown on the first validation error (Severity is equal to XmlSeverityType.Error).


If an element reports a validation error, the rest of the content model for that element is not validated, however, its children are validated. The reader only reports the first error for a given element.

The callback handler can use the ValidationEventArgs.Severity property to guarantee that an XML instance document is validated against a schema. The Severity property allows you to distinguish between validation errors (Severity is equal to XmlSeverityType.Error) that indicate a fatal error, and validation warnings (Severity is equal to XmlSeverityType.Warning) that indicate that no schema information is available.

The following example validates a file against an XML Schema.

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

public class Sample

  private XmlTextReader txtreader = null;
  private XmlValidatingReader reader = null;
  private Boolean m_success = true;

  public Sample ()
        //Validate file against the XSD schema. 
        //The validation should fail.

  public static void Main ()
      Sample validation = new Sample();

  private void Validate(String filename)
        Console.WriteLine("Validating XML file " + filename.ToString());
        txtreader = new XmlTextReader (filename);
        reader = new XmlValidatingReader (txtreader);

        // Set the validation event handler
        reader.ValidationEventHandler += new ValidationEventHandler (this.ValidationEventHandle);

        // Read XML data
        while (reader.Read()){}
        Console.WriteLine ("Validation finished. Validation {0}", (m_success==true ? "successful" : "failed"));

        //Close the reader.
        if (reader != null)

  //Display the validation error.
  private void ValidationEventHandle (object sender, ValidationEventArgs args)
     m_success = false;
     Console.WriteLine("\r\n\tValidation error: " + args.Message );

The sample uses the following two input files:

notValidXSD.xml (The xsi:schemaLocation attribute identifies the XML Schema for the reader.)

<?xml version='1.0'?>
<bookstore xmlns="urn:bookstore-schema"
     xsi:schemaLocation="urn:bookstore-schema books.xsd">
  <book genre="novel">
    <title>The Confidence Man</title>


<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"

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

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

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

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


.NET Framework
Available since 1.1
Return to top