Partilhar via


XML Schema (XSD) Validation with Multiple Schemas

You can use the XmlValidatingReader to validate XML documents against XML Schema definition language (XSD) schemas from multiple schemas.

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 a file stream, Mixed.xml, as input and validates XML fragments against multiple schema sources. The XmlValidatingReader constructor takes a stream of an XML fragment, an XmlNodeType, and XmlParserContext.

Note

In the following example, the top-level element is not validated. The XmlValidatingReader throws a warning for the root element if ValidationType is set to ValidationType.Schema.

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

Namespace ValidationSample
    
   Class Sample
      
      Public Shared Sub Main()
         Dim stream As New FileStream("Mixed.xml", FileMode.Open)
         Dim vr As New XmlValidatingReader(stream, XmlNodeType.Element, Nothing)
         
         vr.Schemas.Add(Nothing, "Book.xsd")
         vr.Schemas.Add(Nothing, "Tape.xsd")
         vr.ValidationType = ValidationType.Schema
         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()
      {
         FileStream stream = new FileStream("Mixed.xml", FileMode.Open);
         XmlValidatingReader vr = new XmlValidatingReader(stream, XmlNodeType.Element, null);

         vr.Schemas.Add(null, "Book.xsd");
         vr.Schemas.Add(null, "Tape.xsd");
         vr.ValidationType = ValidationType.Schema;
         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 an XML Schema file, Book.xsd, to be validated against.

<xs:schema xmlns="urn:bookstore-schema"
         targetNamespace="urn:bookstore-schema"
         xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <xs:element name="book">
      <xs:complexType>
         <xs:simpleContent>
            <xs:extension base="xs:string">
               <xs:attribute name="price" type="xs:decimal" />
            </xs:extension>
         </xs:simpleContent>
      </xs:complexType>
   </xs:element>
</xs:schema>

The following outlines the contents of an XML Schema file, Tape.xsd, to be validated against.

<xs:schema xmlns="urn:tapestore-schema"
         targetNamespace="urn:tapestore-schema"
         xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <xs:element name="tape" type="xs:string"/>
</xs:schema>

The following outlines the contents of the XML stream, Mixed.xml, containing the XML fragments to be validated.

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="urn:dvdstore-schema" targetNamespace="urn:dvdstore-schema">
    <xs:element name="dvd" type="xs:string" />
  </xs:schema>
  <pb:book price="7.99" xmlns:pb="urn:bookstore-schema">The Autobiography of Benjamin Franklin</pb:book>
  <pd:dvd xmlns:pd="urn:dvdstore-schema">The Godfather</pd:dvd>
  <pt:tape xmlns:pt="urn:tapestore-schema" xsi:schemaLocation="urn:tapestore-schema tape.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Il Postino</pt:tape>

See Also

Concepts

Reading XML with the XmlReader

Other Resources

Using the XmlReader Class