Partilhar via


Validation with an Inline XDR Schema

You can use the XmlValidatingReader to validate an XML document against an inline XML-Data Reduced (XDR) schema.

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 the inline XDR schema.

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

Namespace ValidationSample
    
   Class Sample
      
      Public Shared Sub Main()
         Dim tr As New XmlTextReader("HeadCount.xml")
         Dim vr As New XmlValidatingReader(tr)
         
         vr.ValidationType = ValidationType.XDR
         AddHandler vr.ValidationEventHandler, AddressOf ValidationHandler
         
         While vr.Read()
         End While
         Console.WriteLine("Validation finished")
      End Sub
      ' Main

      Public Shared Sub ValidationHandler(sender As Object, args As ValidationEventArgs)
         Console.WriteLine("***Validation error")
         Console.WriteLine("Severity:{0}", args.Severity)
         Console.WriteLine("Message:{0}", args.Message)
      End Sub
      ' ValidationHandler
   End Class
   ' Sample
End Namespace
' ValidationSample
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.

<root>
   <Schema name='xdrHeadCount' xmlns="urn:schemas-microsoft-com:xml-data" xmlns:dt="urn:schemas-microsoft-com:datatypes">
      <ElementType name="Name" content="textOnly"/>
      <ElementType name="HeadCount" content="eltOnly">
         <element type="Name"/>
      </ElementType>
   </Schema>
   <HeadCount xmlns='x-schema:#xdrHeadCount'>
      <Name>Waldo Pepper</Name>
      <Name>Red Pepper</Name>
   </HeadCount>
</root>

The following code example creates an XmlValidatingReader that takes an XmlTextReader. The input file, sample3.xml, is validated against the inline XDR schema. Because there is not an xmlns attribute, the inline schema is specified as the default schema. In this case, the namespace declaration, xmlns="x-schema", is not required.

Dim tr As New XmlTextReader("sample3.xml")
Dim vr As New XmlValidatingReader(tr)
vr.ValidationType = ValidationType.XDR
AddHandler vr.ValidationEventHandler, AddressOf ValidationCallBack
While vr.Read()
   Console.WriteLine("NodeType: {0} NodeName: {1}", vr.NodeType, vr.Name)
End While
XmlTextReader tr = new XmlTextReader("sample3.xml");
XmlValidatingReader vr = new XmlValidatingReader(tr);
vr.ValidationType = ValidationType.XDR;
vr.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
while(vr.Read()) {
    Console.WriteLine("NodeType: {0} NodeName: {1}", vr.NodeType, vr.Name);
    }

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

<root>
    <Schema 
      xmlns="urn:schemas-microsoft-com:xml-data" 
      xmlns:dt="urn:schemas-microsoft-com:datatypes">
        
      <ElementType name='row'>
      </ElementType>

      <ElementType name='data'>
        <element type='row' minOccurs='0' maxOccurs='*'/>
      </ElementType>
    </Schema>
    <data>
      <row/>
      <row/>
    </data>
</root>

See Also

Concepts

Reading XML with the XmlReader

Other Resources

Using the XmlReader Class