다음을 통해 공유


Validation against XML Schema (XSD) with the XmlValidatingReader

XML Schema definition language (XSD) schema validation is implemented using the validity constraints defined in the World Wide Web Consortium (W3C) XML Schema Recommendation. The Microsoft .NET Framework supports the W3C XML Schema 2001 Recommendation. All XML Schemas must include the namespace http://www.w3c.org/2001/XMLSchema.

Each XML Schema is associated with a namespace URI. The XmlValidatingReader can use the XML Schema (XSD) schemaLocation attribute in the data file that is bound to the XML Schema instance namespace, http://www.w3.org/2001/XMLSchema-instance. If the schema is stored in an XmlSchemaCollection, the XmlValidatingReader uses the namespace URI specified when the schema was added to the collection. This is typically the target namespace of the schema.

The XmlValidatingReader uses the schemaLocation attribute to identify the XML Schema. For example, the following shows the root element of the data file.

<bookstore xmlns="urn:bookstore-schema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:bookstore-schema books.xsd">

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

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.Schema
         AddHandler vr.ValidationEventHandler, AddressOf ValidationHandler
         
         While vr.Read()
            PrintTypeInfo(vr)
            If vr.NodeType = XmlNodeType.Element Then
               While vr.MoveToNextAttribute()
                  PrintTypeInfo(vr)
               End While
            End If
         End While
         Console.WriteLine("Validation finished")
      End Sub
      ' Main
      
      
      Public Shared Sub PrintTypeInfo(vr As XmlValidatingReader)
         If Not (vr.SchemaType Is Nothing) Then
            If TypeOf vr.SchemaType Is XmlSchemaDatatype Or TypeOf vr.SchemaType Is XmlSchemaSimpleType Then
               Dim value As Object = vr.ReadTypedValue()
               Console.WriteLine("{0}({1},{2}):{3}", vr.NodeType, vr.Name, value.GetType().Name, value)
            Else
               If TypeOf vr.SchemaType Is XmlSchemaComplexType Then
                  Dim sct As XmlSchemaComplexType = CType(vr.SchemaType, XmlSchemaComplexType)
                  Console.WriteLine("{0}({1},{2})", vr.NodeType, vr.Name, sct.Name)
               End If
            End If
         End If
      End Sub
      ' PrintTypeInfo
       
      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
[C#]
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.Schema;
      vr.ValidationEventHandler += new ValidationEventHandler (ValidationHandler);

      while(vr.Read())
      {
        PrintTypeInfo(vr);
        if(vr.NodeType == XmlNodeType.Element)
        {
          while(vr.MoveToNextAttribute())
            PrintTypeInfo(vr);
        }
      }
      Console.WriteLine("Validation finished");
    }

    public static void PrintTypeInfo(XmlValidatingReader vr)
    {
      if(vr.SchemaType != null)
      {
        if(vr.SchemaType is XmlSchemaDatatype || vr.SchemaType is XmlSchemaSimpleType)
        {
          object value = vr.ReadTypedValue();
          Console.WriteLine("{0}({1},{2}):{3}", vr.NodeType, vr.Name, value.GetType().Name, value);
      }
        else if(vr.SchemaType is XmlSchemaComplexType)
            {
               XmlSchemaComplexType sct = (XmlSchemaComplexType)vr.SchemaType;
               Console.WriteLine("{0}({1},{2})", vr.NodeType, vr.Name, sct.Name);
            }
         }
      }

      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.

<hc:HeadCount xmlns:hc='xsdHeadCount' xsi:schemaLocation='xsdHeadCount HeadCount.xsd' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
  <Name>Waldo Pepper</Name>
  <Name>Red Pepper</Name>
</hc:HeadCount>

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

<xs:schema xmlns="xsdHeadCount" targetNamespace="xsdHeadCount" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name='HeadCount' type="HEADCOUNT"/>
  <xs:complexType name="HEADCOUNT">
    <xs:sequence>
      <xs:element name='Name' type='xs:string' maxOccurs='unbounded'/>
    </xs:sequence>
    <xs:attribute name='division' type='xs:int' use='optional' default='8'/>
  </xs:complexType>
</xs:schema>

See Also

Validation of XML with XmlValidatingReader | Validation of XML with Schemas | XmlSchemaCollection as a Schema Cache