可以使用 XmlValidatingReader 根据内联 XML 数据缩减 (XDR) 架构对 XML 文档进行验证。
![]() |
---|
XmlValidatingReader 类在 .NET Framework 2.0 版 中已过期。您可以使用 XmlReaderSettings 类和 Create 方法创建一个验证 XmlReader 实例。有关更多信息,请参见使用 XmlReader 验证 XML 数据。 |
示例
下面的代码示例创建一个接受 XmlTextReader 的 XmlValidatingReader。 根据内联 XDR 架构对输入文件 HeadCount.xml 进行验证。
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);
}
}
}
下面概括了要验证的输入文件 HeadCount.xml 的内容。
<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>
下面的代码示例创建一个接受 XmlTextReader 的 XmlValidatingReader。 根据内联 XDR 架构对输入文件 sample3.xml 进行验证。 因为没有 xmlns 属性,所以内联架构被指定为默认架构。 这种情况下,不需要命名空间声明 xmlns="x-schema"。
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);
}
下面概括了要验证的输入文件 sample3.xml 的内容。
<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>