XmlValidatingReader.ValidationEventHandler Event


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

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 As 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.

Imports System
Imports System.IO
Imports System.Xml
Imports System.Xml.Schema
Imports Microsoft.VisualBasic

Public Class Sample

    Private txtreader As XmlTextReader = Nothing
    Private reader As XmlValidatingReader = Nothing
    Private m_success As Boolean = True

    Public Sub New()
        'Validate file against the XSD schema. 
        'The validation should fail.
    End Sub 'NewNew

    Public Shared Sub Main()
        Dim validation As New Sample()
    End Sub 'Main

    Private Sub Validate(filename As String)
            Console.WriteLine("Validating XML file " & filename.ToString())
            txtreader = New XmlTextReader(filename)
            reader = New XmlValidatingReader(txtreader)

            ' Set the validation event handler
            AddHandler reader.ValidationEventHandler, AddressOf Me.ValidationEventHandle

            ' Read XML data
            While reader.Read()
            End While
            Console.WriteLine("Validation finished. Validation {0}", IIf(m_success, "successful", "failed"))

            'Close the reader.
            If Not (reader Is Nothing) Then
            End If
        End Try
    End Sub 'Validate

    'Display the validation error.
    Private Sub ValidationEventHandle(sender As Object, args As ValidationEventArgs)
        m_success = False
        Console.WriteLine(ControlChars.CrLf & ControlChars.Tab & "Validation error: " & args.Message)
    End Sub 'ValidationEventHandle
End Class '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