XmlValidatingReader Constructor (XmlReader)

XmlValidatingReader Constructor (XmlReader)


Initializes a new instance of the XmlValidatingReader class that validates the content returned from the given XmlReader.

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

public XmlValidatingReader(
	XmlReader reader


Type: System.Xml.XmlReader

The XmlReader to read from while validating. The current implementation supports only XmlTextReader.

Exception Condition

The reader specified is not an XmlTextReader.


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.

All nodes returned from the given XmlReader are also returned from this validating reader, so there is no information loss in the process. New nodes not returned from the underlying reader may be added by this reader (for example, default attributes and the children of an entity reference). Any properties set on the given XmlTextReader also apply to this validating reader. For example, if the supplied reader had WhitespaceHandling.None set, this validating reader also ignores white space.

When external document type definitions (DTDs) or schemas are needed for validation, the XmlResolver property sets the XmlResolver object to use for resolving external resources.

The following example validates two documents.

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

public class Sample

  private Boolean m_success = true;

  public Sample ()
      //Validate the document using an external XSD schema.  Validation should fail.

      //Validate the document using an inline XSD. Validation should succeed.

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

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

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

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

      //Close the reader.

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

The sample uses the following input files:


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



<!--Inline XSD schema-->
<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="price"  type="xsd:decimal"/>
  <xsd:attribute name="genre" type="xsd:string"/>
<!-- end of schema -->

  <book genre="novel">
    <title>Pride And Prejudice</title>

.NET Framework
Available since 1.1
Return to top
© 2016 Microsoft