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)

event ValidationEventHandler^ ValidationEventHandler {
	void add(ValidationEventHandler^ value);
	void remove(ValidationEventHandler^ value);


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.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Schema;
public ref class Sample
   XmlTextReader^ txtreader;
   XmlValidatingReader^ reader;
   Boolean m_success;

      txtreader = nullptr;
      reader = nullptr;
      m_success = true;

      //Validate file against the XSD schema. 
      //The validation should fail.
      Validate( "notValidXSD.xml" );

   void Validate( String^ filename )
         Console::WriteLine( "Validating XML file {0}", filename );
         txtreader = gcnew XmlTextReader( filename );
         reader = gcnew XmlValidatingReader( txtreader );

         // Set the validation event handler
         reader->ValidationEventHandler += gcnew ValidationEventHandler( this, &Sample::ValidationEventHandle );

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

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


   //Display the validation error.
   void ValidationEventHandle( Object^ /*sender*/, ValidationEventArgs^ args )
      m_success = false;
      Console::WriteLine( "\r\n\tValidation error: {0}", args->Message );


int main()
   gcnew Sample;

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