XmlDocument.Validate 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
驗證該 XmlDocument 特性中 Schemas 包含的 XML 結構定義語言(XSD)架構。
多載
| 名稱 | Description |
|---|---|
| Validate(ValidationEventHandler) |
驗證該 XmlDocument 特性中 Schemas 包含的 XML 結構定義語言(XSD)架構。 |
| Validate(ValidationEventHandler, XmlNode) |
Validate(ValidationEventHandler)
驗證該 XmlDocument 特性中 Schemas 包含的 XML 結構定義語言(XSD)架構。
public:
void Validate(System::Xml::Schema::ValidationEventHandler ^ validationEventHandler);
public void Validate(System.Xml.Schema.ValidationEventHandler? validationEventHandler);
public void Validate(System.Xml.Schema.ValidationEventHandler validationEventHandler);
member this.Validate : System.Xml.Schema.ValidationEventHandler -> unit
Public Sub Validate (validationEventHandler As ValidationEventHandler)
參數
- validationEventHandler
- ValidationEventHandler
ValidationEventHandler接收結構驗證警告與錯誤資訊的物件。
例外狀況
發生了結構驗證事件,但未 ValidationEventHandler 指定物件。
範例
以下範例說明了該 Validate 方法的使用。 範例中建立 XmlDocument 包含相關 XSD 結構的 ,使用 XmlReaderSettings 和 XmlReader 物件。 接著範例會利用該 XPathNavigator 類別錯誤修改 XML 文件中元素的型別值,產生結構驗證錯誤。
using System;
using System.Xml;
using System.Xml.Schema;
using System.Xml.XPath;
class XPathValidation
{
static void Main()
{
try
{
XmlReaderSettings settings = new XmlReaderSettings();
settings.Schemas.Add("http://www.contoso.com/books", "contosoBooks.xsd");
settings.ValidationType = ValidationType.Schema;
XmlReader reader = XmlReader.Create("contosoBooks.xml", settings);
XmlDocument document = new XmlDocument();
document.Load(reader);
ValidationEventHandler eventHandler = new ValidationEventHandler(ValidationEventHandler);
// the following call to Validate succeeds.
document.Validate(eventHandler);
// add a node so that the document is no longer valid
XPathNavigator navigator = document.CreateNavigator();
navigator.MoveToFollowing("price", "http://www.contoso.com/books");
XmlWriter writer = navigator.InsertAfter();
writer.WriteStartElement("anotherNode", "http://www.contoso.com/books");
writer.WriteEndElement();
writer.Close();
// the document will now fail to successfully validate
document.Validate(eventHandler);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
static void ValidationEventHandler(object sender, ValidationEventArgs e)
{
switch (e.Severity)
{
case XmlSeverityType.Error:
Console.WriteLine("Error: {0}", e.Message);
break;
case XmlSeverityType.Warning:
Console.WriteLine("Warning {0}", e.Message);
break;
}
}
}
Imports System.Xml
Imports System.Xml.Schema
Imports System.Xml.XPath
Class XPathValidation
Shared Sub Main()
Try
Dim settings As XmlReaderSettings = New XmlReaderSettings()
settings.Schemas.Add("http://www.contoso.com/books", "contosoBooks.xsd")
settings.ValidationType = ValidationType.Schema
Dim reader As XmlReader = XmlReader.Create("contosoBooks.xml", settings)
Dim document As XmlDocument = New XmlDocument()
document.Load(reader)
Dim eventHandler As ValidationEventHandler = New ValidationEventHandler(AddressOf ValidationEventHandler)
' the following call to Validate succeeds.
document.Validate(eventHandler)
' add a node so that the document is no longer valid
Dim navigator As XPathNavigator = document.CreateNavigator()
navigator.MoveToFollowing("price", "http://www.contoso.com/books")
Dim writer As XmlWriter = navigator.InsertAfter()
writer.WriteStartElement("anotherNode", "http://www.contoso.com/books")
writer.WriteEndElement()
writer.Close()
' the document will now fail to successfully validate
document.Validate(eventHandler)
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Sub
Shared Sub ValidationEventHandler(ByVal sender As Object, ByVal e As ValidationEventArgs)
Select Case e.Severity
Case XmlSeverityType.Error
Console.WriteLine("Error: {0}", e.Message)
Case XmlSeverityType.Warning
Console.WriteLine("Warning {0}", e.Message)
End Select
End Sub
End Class
範例中輸入 和 contosoBooks.xmlcontosoBooks.xsd 檔案。
<?xml version="1.0" encoding="utf-8" ?>
<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 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>
備註
此Validate方法會根據屬性中包含的 Schemas XML 資料XmlDocument進行驗證。 此 Validate 方法執行資料集擴充。 具體來說,在成功驗證後,會套用結構預設值,視需要將文字值轉換為原子值,並將型別資訊與驗證過的資訊項目關聯。 結果是一個先前未打型的 XML 子樹,但 XmlDocument 被替換為一個有型別的子樹。
使用此 Validate 方法時請考慮以下幾點:
- 結構位置提示
xsi:schemaLocationxsi:noNamespaceSchemaLocation會忽略或類似。 - 內嵌結構則被忽略。
- 若在驗證過程中發生結構驗證錯誤,部分 XmlDocument 節點的型別資訊正確,部分則不正確。
- 驗證過程包括檢查唯一性及參考約束(
xs:ID、xs:keyrefxs:IDREFxs:keyxs:unique、 )。
適用於
Validate(ValidationEventHandler, XmlNode)
public:
void Validate(System::Xml::Schema::ValidationEventHandler ^ validationEventHandler, System::Xml::XmlNode ^ nodeToValidate);
public void Validate(System.Xml.Schema.ValidationEventHandler? validationEventHandler, System.Xml.XmlNode nodeToValidate);
public void Validate(System.Xml.Schema.ValidationEventHandler validationEventHandler, System.Xml.XmlNode nodeToValidate);
member this.Validate : System.Xml.Schema.ValidationEventHandler * System.Xml.XmlNode -> unit
Public Sub Validate (validationEventHandler As ValidationEventHandler, nodeToValidate As XmlNode)
參數
- validationEventHandler
- ValidationEventHandler
ValidationEventHandler接收結構驗證警告與錯誤資訊的物件。
- nodeToValidate
- XmlNode
XmlNode由驗證XmlDocument所建立的物件。
例外狀況
XmlNode物件參數並非由 XmlDocument.
XmlNode物件參數不是元素、屬性、文件片段或根節點。
發生了結構驗證事件,但未 ValidationEventHandler 指定物件。
範例
備註
此方法會 Validate 驗證物件中的 XmlNode XML 資料,與該 Schemas 屬性中包含的結構相符。 此 Validate 方法執行資料集擴充。 具體來說,在成功驗證後,會套用結構預設值,視需要將文字值轉換為原子值,並將型別資訊與驗證過的資訊項目關聯。 結果是一個先前未打型的 XML 子樹,但 XmlDocument 被替換為一個有型別的子樹。
以下是使用此 Validate 方法時需注意的重要事項。
結構位置提示
xsi:schemaLocationxsi:noNamespaceSchemaLocation會忽略或類似。內嵌結構則被忽略。
若在驗證過程中發生結構驗證錯誤,部分 XmlDocument 節點的型別資訊正確,部分則不正確。
若驗證節點為根節點,驗證過程包含檢查唯一性與參考約束(xs:ID, xs:keyrefxs:IDREFxs:key及xs:unique);否則,唯一性與參考約束將省略。