Share via


Validation against XDR with the XmlValidatingReader

XML-Data Reduced (XDR) schema validation is implemented using the validity constraints defined in the Microsoft XML Parser (MSXML) schema specification. The XmlValidatingReader class uses the x-schema: namespace declaration in an XML document to determine that the schema to be validated against is an XDR schema. In the following example, the root element of the data file is <HeadCount xmlns='x-schema:HeadCount.xdr'>.

Note

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

Example

The following code example creates an XmlValidatingReader that takes an XmlTextReader. The input file, HeadCount.xml, is validated against an XDR schema file, HeadCount.xdr.

Imports System
Imports System.IO
Imports System.Xml
Imports System.Xml.Schema

public class ValidationSample

      public shared sub Main()

         Dim tr As XmlTextReader = new XmlTextReader("HeadCount.xml")
         Dim vr As XmlValidatingReader = new XmlValidatingReader(tr)

         vr.ValidationType = ValidationType.XDR
         AddHandler vr.ValidationEventHandler, AddressOf ValidationCallback      

         while(vr.Read())
         end while
         Console.WriteLine("Validation finished")
     end sub

      public shared sub ValidationCallBack(sender As object, args as ValidationEventArgs)
         Console.WriteLine("***Validation error")
         Console.WriteLine("Severity:{0}", args.Severity)
         Console.WriteLine("Message:{0}", args.Message)
     end sub
end class
using System;
using System.IO;
using System.Xml;
using System.Xml.Schema;

namespace ValidationSample
{
   class Sample
   {
      public static void Main()
      {
         XmlTextReader tr = new XmlTextReader("HeadCount.xml");
         XmlValidatingReader vr = new XmlValidatingReader(tr);

         vr.ValidationType = ValidationType.XDR;
         vr.ValidationEventHandler += new ValidationEventHandler (ValidationHandler);

         while(vr.Read());
         Console.WriteLine("Validation finished");
      }

      public static void ValidationHandler(object sender, ValidationEventArgs args)
      {
         Console.WriteLine("***Validation error");
         Console.WriteLine("\tSeverity:{0}", args.Severity);
         Console.WriteLine("\tMessage  :{0}", args.Message);
      }
   }
}

The following outlines the contents of the input file, HeadCount.xml, to be validated.

<HeadCount xmlns='x-schema:HeadCount.xdr'>
   <Name>Waldo Pepper</Name>
   <Name>Red Pepper</Name>
</HeadCount>

The following outlines the contents of the XDR schema file, HeadCount.xdr, to be validated against.

<Schema xmlns="urn:schemas-microsoft-com:xml-data" xmlns:dt="urn:schemas-microsoft-com:datatypes">
   <ElementType name="Name" content="textOnly"/>
   <AttributeType name="Bldg" default="2"/>
   <ElementType name="HeadCount" content="eltOnly">
      <element type="Name"/>
      <attribute type="Bldg"/>
   </ElementType>
</Schema>

See Also

Concepts

Reading XML with the XmlReader

Other Resources

Using the XmlReader Class