使用 XmlSchemaCollection 的 XDR 驗證
如果您據以驗證的 XML 資料精簡 (XDR) 結構描述是儲存在 XmlSchemaCollection 中,它會和當將結構描述加入集合時指定的命名空間 URI 建立關聯。XmlValidatingReader 會將 XML 文件中的命名空間 URI 對應至集合中與這個 URI 對應的結構描述。
重要資訊 |
---|
XmlSchemaCollection 類別目前已過時,並已由 XmlSchemaSet 類別取代。如需 XmlSchemaSet 類別的詳細資訊,請參閱用於結構描述編譯的 XmlSchemaSet。 |
例如,若 XML 文件的根項目是 <bookstore xmlns="urn:newbooks-schema">
,則將結構描述加入 XmlSchemaCollection 時,它會參照相同的命名空間,如下所示:
xsc.Add("urn:newbooks-schema", "newbooks.xdr")
下列程式碼範例會建立使用 XmlTextReader 的 XmlValidatingReader,並且將 XDR 結構描述 HeadCount.xdr 加入 XmlSchemaCollection 中。
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.Schemas.Add("xdrHeadCount", "HeadCount.xdr")
vr.ValidationType = ValidationType.XDR
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)
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
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.Schemas.Add("xdrHeadCount", "HeadCount.xdr");
vr.ValidationType = ValidationType.XDR;
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);
}
}
}
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) 內容。
<!--Load HeadCount.xdr in SchemaCollection for Validation-->
<HeadCount xmlns='xdrHeadCount'>
<Name>Waldo Pepper</Name>
<Name>Red Pepper</Name>
</HeadCount>
以下描述據以驗證的 XDR 結構描述檔案 HeadCount.xdr 的內容。
<Schema xmlns="urn:schemas-microsoft-com:xml-data" xmlns:dt="urn:schemas-microsoft-com:datatypes">
<ElementType name="Name" content="textOnly"/>
<AttributeType name="Bldg" default="2"/>
<ElementType name="HeadCount" content="eltOnly">
<element type="Name"/>
<attribute type="Bldg"/>
</ElementType>
</Schema>