.NET Framework Class Library
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)
Syntax

Visual Basic (Declaration)
Public Sub New ( _
    reader As XmlReader _
)
Visual Basic (Usage)
Dim reader As XmlReader

Dim instance As New XmlValidatingReader(reader)
C#
public XmlValidatingReader(
    XmlReader reader
)
Visual C++
public:
XmlValidatingReader(
    XmlReader^ reader
)
JScript
public function XmlValidatingReader(
    reader : XmlReader
)

Parameters

reader
Type: System.Xml..::.XmlReader
The XmlReader to read from while validating. The current implementation supports only XmlTextReader.
Exceptions

ExceptionCondition
ArgumentException

The reader specified is not an XmlTextReader.

Remarks

NoteNote:

The XmlValidatingReader class is obsolete in .NET Framework version 2.0. You can create a validating XmlReader instance by using the XmlReaderSettings class and the Create method. For more information, see Validating XML Data with XmlReader.

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.

Examples

The following example validates two documents.

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

public class Sample

  private m_success as Boolean = true

  public sub New ()
      'Validate the document using an external XSD schema.  Validation should fail.
      Validate("notValidXSD.xml") 

      'Validate the document using an inline XSD. Validation should succeed.
      Validate("inlineXSD.xml")
  end sub

  public shared sub Main ()

      Dim validation as Sample = new Sample()
  end sub

  private sub Validate(filename as String)

      m_success = true
      Console.WriteLine()
      Console.WriteLine("******")
      Console.WriteLine("Validating XML file " + filename.ToString())
      Dim txtreader as XmlTextReader = new XmlTextReader (filename)
      Dim reader as XmlValidatingReader = new XmlValidatingReader (txtreader)

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

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

      'Close the reader.
      reader.Close()
  end sub

  'Display the validation error.
  Private sub ValidationCallBack (sender as object, args as ValidationEventArgs)

     m_success = false
     Console.WriteLine()
     Console.WriteLine("  Validation error: " + args.Message )
  end sub
end class
C#
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("notValidXSD.xml"); 

      //Validate the document using an inline XSD. Validation should succeed.
      Validate("inlineXSD.xml");
  }    

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

  private void Validate(String filename)
  {    
      m_success = true;
      Console.WriteLine("\r\n******");
      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.
      reader.Close();
  }

  //Display the validation error.
  private void ValidationCallBack (object sender, ValidationEventArgs args)
  {
     m_success = false;
     Console.WriteLine("\r\n\tValidation error: " + args.Message );
  }
}
Visual C++
#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Schema;
public ref class Sample
{
private:
   static Boolean m_success = true;

public:
   Sample()
   {

      // Validate the document using an external XSD schema.  Validation should fail.
      Validate( "notValidXSD.xml" );

      // Validate the document using an inline XSD. Validation should succeed.
      Validate( "inlineXSD.xml" );
   }


private:

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

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

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

      // Read XML data
      while ( reader->Read() )
      {}

      Console::WriteLine( "Validation finished. Validation {0}", (m_success == true ? (String^)"successful!" : "failed.") );

      // Close the reader.
      reader->Close();
   }

};

int main()
{
   Sample^ validation = gcnew Sample;
}

CPP_OLD
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Schema;

__gc public class Sample
{
private:
   static Boolean m_success = true;

public:
   Sample () 
   {
      // Validate the document using an external XSD schema.  Validation should fail.
      Validate(S"notValidXSD.xml");

      // Validate the document using an inline XSD. Validation should succeed.
      Validate(S"inlineXSD.xml");
   }
private:

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


   void Validate(String * filename)
   {
      m_success = true;
      Console::WriteLine(S"\r\n******");
      Console::WriteLine(S"Validating XML file {0}", filename);
      XmlTextReader* txtreader = new XmlTextReader (filename);
      XmlValidatingReader* reader = new XmlValidatingReader (txtreader);

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

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

      // Close the reader.
      reader -> Close();
   }
};

int main()
{
   Sample* validation = new Sample();
}

The sample uses the following input files:

notValidXSD.xml

None
<?xml version='1.0'?>
<bookstore xmlns="urn:bookstore-schema"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="urn:bookstore-schema books.xsd">
  <book>
    <author>
      <first-name>Benjamin</first-name>
      <last-name>Franklin</last-name>
    </author>
  </book>
  <book genre="novel">
    <title>The Confidence Man</title>
    <author>
      <first-name>Herman</first-name>
      <last-name>Melville</last-name>
    </author>
    <price>11.99</price>
  </book>
</bookstore>

books.xsd

None
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns="urn:bookstore-schema"
    elementFormDefault="qualified"
    targetNamespace="urn:bookstore-schema">

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

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

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

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

</xsd:schema>

inlineXSD.xml

None
<store-data>
<!--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:sequence>
 </xsd:complexType>

 <xsd:complexType name="bookType">
  <xsd:sequence>
   <xsd:element name="title" type="xsd:string"/>
   <xsd:element name="price"  type="xsd:decimal"/>
  </xsd:sequence>
  <xsd:attribute name="genre" type="xsd:string"/>
 </xsd:complexType>
</xsd:schema>
<!-- end of schema -->

<bookstore>
  <book genre="novel">
    <title>Pride And Prejudice</title>
    <price>19.95</price>
  </book>
</bookstore>
</store-data>
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
See Also

Reference

Other Resources

Tags :


Page view tracker