XML 文件可以在 XmlSchemaSet 中依據 XML 架構定義語言 (XSD) 架構進行驗證。
驗證 XML 檔
XML 文件是由Create類別的XmlReader方法驗證。 若要驗證 XML 檔,請建構 XmlReaderSettings 物件,其中包含用來驗證 XML 檔的 XML 架構定義語言 (XSD) 架構。
備註
命名空間System.Xml.Schema包含擴充方法,可讓您在使用LINQ to XML (C#) 和LINQ to XML (Visual Basic) 時,輕鬆地針對 XSD 檔案驗證 XML 樹狀結構。 如需使用 LINQ to XML 驗證 XML 檔案的詳細資訊,請參閱 如何使用 XSD 進行驗證 (LINQ to XML) (C#) 和 如何:使用 XSD 驗證 (LINQ to XML) (Visual Basic) 。
您可以將單一架構XmlSchemaSet或一組架構新增至XmlSchemaSet,方法是將其中一個作為參數傳遞至Add的XmlSchemaSet方法。 驗證檔時,檔的目標命名空間必須符合架構集中架構的目標命名空間。
以下是 XML 檔的範例。
<bookstore xmlns="http://www.contoso.com/books">
<book genre="autobiography" publicationdate="1981-03-22" ISBN="1-861003-11-0">
<title>The Autobiography of Benjamin Franklin</title>
<author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
<price>8.99</price>
</book>
<book genre="novel" publicationdate="1967-11-17" ISBN="0-201-63361-2">
<title>The Confidence Man</title>
<author>
<first-name>Herman</first-name>
<last-name>Melville</last-name>
</author>
<price>11.99</price>
</book>
<book genre="philosophy" publicationdate="1991-02-15" ISBN="1-861001-57-6">
<title>The Gorgias</title>
<author>
<name>Plato</name>
</author>
<price>9.99</price>
</book>
</bookstore>
以下是驗證範例 XML 檔的架構。
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.contoso.com/books" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="bookstore">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="book">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string" />
<xs:element name="author">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="name" type="xs:string" />
<xs:element minOccurs="0" name="first-name" type="xs:string" />
<xs:element minOccurs="0" name="last-name" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="price" type="xs:decimal" />
</xs:sequence>
<xs:attribute name="genre" type="xs:string" use="required" />
<xs:attribute name="publicationdate" type="xs:date" use="required" />
<xs:attribute name="ISBN" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
在以下的程式碼範例中,以上的結構會新增到Schemas物件的XmlReaderSettings屬性。 物件 XmlReaderSettings 會當做參數傳遞至 Create 物件的 方法 XmlReader ,以驗證上述 XML 檔。
物件的 ValidationType 屬性 XmlReaderSettings 會設定為 Schema
,以透過 Create 物件的 方法 XmlReader 強制執行 XML 檔的驗證。
ValidationEventHandler會被新增到XmlReaderSettings物件中,以處理在 XML 文件和架構的驗證過程中發現的錯誤所引發的任何Warning或Error事件。
using System;
using System.Xml;
using System.Xml.Schema;
class XmlSchemaSetExample
{
static void Main()
{
XmlReaderSettings booksSettings = new XmlReaderSettings();
booksSettings.Schemas.Add("http://www.contoso.com/books", "books.xsd");
booksSettings.ValidationType = ValidationType.Schema;
booksSettings.ValidationEventHandler += booksSettingsValidationEventHandler;
XmlReader books = XmlReader.Create("books.xml", booksSettings);
while (books.Read()) { }
}
static void booksSettingsValidationEventHandler(object sender, ValidationEventArgs e)
{
if (e.Severity == XmlSeverityType.Warning)
{
Console.Write("WARNING: ");
Console.WriteLine(e.Message);
}
else if (e.Severity == XmlSeverityType.Error)
{
Console.Write("ERROR: ");
Console.WriteLine(e.Message);
}
}
}
Imports System.Xml
Imports System.Xml.Schema
Class XmlSchemaSetExample
Shared Sub Main()
Dim booksSettings As XmlReaderSettings = New XmlReaderSettings()
booksSettings.Schemas.Add("http://www.contoso.com/books", "books.xsd")
booksSettings.ValidationType = ValidationType.Schema
AddHandler booksSettings.ValidationEventHandler, New ValidationEventHandler(AddressOf booksSettingsValidationEventHandler)
Dim books As XmlReader = XmlReader.Create("books.xml", booksSettings)
While books.Read()
End While
End Sub
Shared Sub booksSettingsValidationEventHandler(ByVal sender As Object, ByVal e As ValidationEventArgs)
If e.Severity = XmlSeverityType.Warning Then
Console.Write("WARNING: ")
Console.WriteLine(e.Message)
ElseIf e.Severity = XmlSeverityType.Error Then
Console.Write("ERROR: ")
Console.WriteLine(e.Message)
End If
End Sub
End Class