XmlValidatingReader.ValidationType Property


Gets or sets a value indicating the type of validation to perform.

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

Public Property ValidationType As ValidationType

Property Value

Type: System.Xml.ValidationType

One of the ValidationType values. If this property is not set, it defaults to ValidationType.Auto.

Exception Condition

Setting the property after a Read has been called.


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.

This property must be set before the first call to Read. Setting this property to ValidationType.None creates a non-validating reader.

If external document type definitions (DTDs) or schemas are needed for validation, the XmlResolver is used.

The following example validates two files.

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

Public Class Sample
    Private doc1 As String = "notValid.xml"
    Private doc2 As String = "cdDTD.xml"
    Private doc3 As String = "book1.xml"

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

    Public Sub New()
        'Parse the files and validate when requested.
        Validate(doc1, ValidationType.XDR) 'Validation should fail.
        Validate(doc2, ValidationType.DTD) 'Validation should fail.
        Validate(doc3, ValidationType.None) 'No validation performed.
    End Sub 'NewNew

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

    Private Sub Validate(filename As String, vt As ValidationType)
            'Implement the readers.  Set the ValidationType.
            txtreader = New XmlTextReader(filename)
            reader = New XmlValidatingReader(txtreader)
            reader.ValidationType = vt

            'If the reader is set to validate, set the event handler.
            If vt = ValidationType.None Then
                Console.WriteLine(ControlChars.Cr & "Parsing XML file " & filename.ToString())
                Console.WriteLine(ControlChars.Cr & "Validating XML file " & filename.ToString())
                m_success = True
                'Set the validation event handler.
                AddHandler reader.ValidationEventHandler, AddressOf ValidationCallBack
            End If

            ' Read XML data
            While reader.Read()
            End While 
            If vt = ValidationType.None Then
                Console.WriteLine("Finished parsing file.")
                Console.WriteLine("Validation finished. Validation {0}", IIf(m_success, "successful", "failed"))
            End If

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

    'Display the validation errors.
    Private Sub ValidationCallBack(sender As Object, args As ValidationEventArgs)
        m_success = False

        Console.Write(ControlChars.CrLf & ControlChars.Tab & "Validation error: " & args.Message)
    End Sub 'ValidationCallBack
End Class 'Sample

The sample uses the following four input files:

notValid.xml (The "x-schema:" prefix identifies the XML-Data Reduced (XDR) schema for the reader.)

<?xml version='1.0'?>
<bookstore xmlns="x-schema:schema1.xdr">
  <book genre="novel" style="hardcover">
    <title>The Handmaid's Tale</title>
  <book genre="novel" style="other">
    <title>The Poisonwood Bible</title>


<?xml version="1.0"?>
 <Schema xmlns="urn:schemas-microsoft-com:xml-data"
   <ElementType name="first-name" content="textOnly"/>
   <ElementType name="last-name" content="textOnly"/>
   <ElementType name="name" content="textOnly"/>
   <ElementType name="price" content="textOnly" dt:type="fixed.14.4"/>
   <ElementType name="author" content="eltOnly" order="one">
     <group order="seq">
       <element type="name"/>
     <group order="seq">
       <element type="first-name"/>
       <element type="last-name"/>
   <ElementType name="title" content="textOnly"/>
   <AttributeType name="genre" dt:type="string"/>
   <AttributeType name="style" dt:type="enumeration"
         dt:values="paperback hardcover"/>
   <ElementType name="book" content="eltOnly">
     <attribute type="genre" required="yes"/>
     <attribute type="style" required="yes"/>
     <element type="title"/>
     <element type="author"/>
     <element type="price"/>
   <ElementType name="bookstore" content="eltOnly">
     <element type="book"/>


<!--XML file using a DTD-->
<!DOCTYPE bookstore [
  <!ELEMENT bookstore (cd)*> 
  <!ELEMENT cd (title,artist,price)>
  <!ELEMENT title (#PCDATA)>
  <!ELEMENT artist (#PCDATA)>
  <!ELEMENT price (#PCDATA)>]>
  <cd genre="alternative"  ISBN="2-3631-4">


<?xml version='1.0' ?>
<!DOCTYPE book [<!ENTITY h 'hardcover'>]>
  <title>Pride And Prejudice</title>

.NET Framework
Available since 1.1
Return to top