使用内联 XML 架构 (XSD) 进行验证

更新:November 2007

可以使用 XmlValidatingReader 根据内联 XML 架构定义语言 (XSD) 架构进行验证。

说明:

XmlValidatingReader 类在 .NET Framework 2.0 版 中已过期。 您可以使用 XmlReaderSettings 类和 Create 方法创建一个验证 XmlReader 实例。 有关更多信息,请参见使用 XmlReader 验证 XML 数据

示例

下面的代码示例创建一个接受 XmlTextReaderXmlValidatingReader。 根据内联 XML 架构对输入文件 HeadCount.xml 进行验证。

说明:

因为内联架构作为根元素的子元素出现,所以当执行内联架构验证时无法验证根元素。 如果 ValidationType 属性设置为 Schema,XmlValidatingReader 将对根元素引发警告。

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++;
      }
   }
}

下面概括了要验证的输入文件 HeadCount.xml 的内容。

<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>

下面的代码示例创建一个接受 XmlTextReaderXmlValidatingReader。 根据内联 XML 架构对输入文件 Sample5.xml 进行验证。

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);
    }

下面概括了要验证的输入文件 Sample5.xml 的内容。

<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>

请参见

概念

用 XmlReader 读取 XML

其他资源

使用 XmlReader 类