Partilhar via


Validation with an Inline XML Schema (XSD)

You can validate against an inline XML Schema definition language (XSD) schema using the XmlValidatingReader.

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 XML Schema.

Note

Because the inline schema appears as a child element of the root element, the root element cannot be validated when inline schema validation is performed. The XmlValidatingReader throws a warning for the root element if the ValidationType property is set to Schema.

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


Namespace ValidationSample
    
   Class Sample
      Private Shared _ValidationErrorsCount As Integer = 0
      
      
      Public Shared Sub Main()
         Dim stream As New FileStream("HeadCount.xml", FileMode.Open)
         Dim vr As New XmlValidatingReader(stream, XmlNodeType.Element, Nothing)
         
         vr.ValidationType = ValidationType.Schema
         AddHandler vr.ValidationEventHandler, AddressOf ValidationHandler
         
         While vr.Read()
         End While
         Console.WriteLine("Validation finished: {0} validation errors", _ValidationErrorsCount)
      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)
         _ValidationErrorsCount += 1
      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
   {
      static int _ValidationErrorsCount = 0;

      public static void Main()
      {
         FileStream stream = new FileStream("HeadCount.xml", FileMode.Open);
         XmlValidatingReader vr = new XmlValidatingReader(stream, XmlNodeType.Element, null);

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

         while(vr.Read());
         Console.WriteLine("Validation finished: {0} validation errors", _ValidationErrorsCount);
      }

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

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

<root>
<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'
            xmlns='xsdHeadCount'
            targetNamespace='xsdHeadCount'>
    <xs:element name='HeadCount'>
        <xs:complexType>
            <xs:sequence>
                <xs:element name='Name' type='xs:string' maxOccurs='unbounded'/>
            </xs:sequence>
            <xs:attribute name='division' type='xs:string' use='optional' default='QA'/>
        </xs:complexType>
    </xs:element>
</xs:schema>
<hc:HeadCount xmlns:hc='xsdHeadCount'>
    <Name>Waldo Pepper</Name>
    <Name>Red Pepper</Name>
</hc:HeadCount>
</root>

The following code example creates an XmlValidatingReader that takes an XmlTextReader. The input file, Sample5.xml, is validated against the inline XML Schema.

Dim tr As New XmlTextReader("Sample5.xml")
Dim vr As New XmlValidatingReader(tr)
vr.ValidationType = ValidationType.Schema
AddHandler vr.ValidationEventHandler, AddressOf ValidationCallBack
While vr.Read()
   Console.WriteLine("NodeType: {0} NodeName: {1}", vr.NodeType, vr.Name)
End While
XmlTextReader tr = new XmlTextReader("Sample5.xml");
XmlValidatingReader vr = new XmlValidatingReader(tr);
vr.ValidationType = ValidationType.Schema;
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, Sample5.xml, to be validated.

<test>
  <schema targetNamespace='test' xmlns='http://www.w3.org/2001/XMLSchema' > 
    <element name='zip' type='positiveInteger'/> 
  </schema>

  <t:zip   xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'  xmlns:t='test'>
    123
  </t:zip>
</test>

See Also

Concepts

Reading XML with the XmlReader

Other Resources

Using the XmlReader Class