XmlDocument.Validate 方法

定義

根據 Schemas 屬性中包含的 XML 結構描述定義語言 (XSD) 結構描述,驗證 XmlDocument

多載

Validate(ValidationEventHandler)

根據 Schemas 屬性中包含的 XML 結構描述定義語言 (XSD) 結構描述,驗證 XmlDocument

Validate(ValidationEventHandler, XmlNode)

根據 Schemas 屬性中包含的 XML 結構描述定義語言 (XSD) 結構描述,驗證指定的 XmlNode 物件。

Validate(ValidationEventHandler)

Source:
XmlDocument.cs
Source:
XmlDocument.cs
Source:
XmlDocument.cs

根據 Schemas 屬性中包含的 XML 結構描述定義語言 (XSD) 結構描述,驗證 XmlDocument

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 其中包含使用 XmlReaderSettingsXmlReader 物件的相關聯 XSD 架構。 然後,此範例會 XPathNavigator 使用 類別,錯誤地修改 XML 檔中產生架構驗證錯誤的元素具型別值。

#using <System.Xml.dll>

using namespace System;
using namespace System::Xml;
using namespace System::Xml::Schema;
using namespace System::Xml::XPath;

class XPathValidation
{
public:

    static void Main()
    {
        try
        {
            XmlReaderSettings^ settings = gcnew XmlReaderSettings();
            settings->Schemas->Add("http://www.contoso.com/books", "contosoBooks.xsd");
            settings->ValidationType = ValidationType::Schema;

            XmlReader^ reader = XmlReader::Create("contosoBooks.xml", settings);
            XmlDocument^ document = gcnew XmlDocument();
            document->Load(reader);

            ValidationEventHandler^ eventHandler = gcnew ValidationEventHandler(ValidationEventHandlerOne);

            // 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 ValidationEventHandlerOne(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;
        }

    }
};

int main()
{
    XPathValidation::Main();
    Console::ReadLine();
    return 0;
};
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.xml 採用 和 contosoBooks.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 架構,驗證 中的 XmlDocument XML 資料。 方法 Validate 會執行資訊集擴增。 具體來說,成功驗證之後,會套用架構預設值、視需要將文字值轉換成不可部分完成的值,而且類型資訊會與已驗證的資訊專案相關聯。 結果是先前以具型別子樹取代的 XmlDocument 中未具型別的 XML 子樹狀結構。

以下是使用 Validate 方法時要考慮的重要注意事項。

  • xsi:noNamespaceSchemaLocation 之類的 xsi:schemaLocation 架構位置提示會被忽略。

  • 會忽略內嵌架構。

  • 如果在驗證期間發生架構驗證錯誤,則會 XmlDocument 部分使用具有正確型別資訊的節點進行部分驗證,有些則沒有 。

  • 驗證程式包括檢查唯一性和參考條件約束 (xs:ID 、、 xs:IDREFxs:keyxs:keyrefxs:unique) 。

適用於

Validate(ValidationEventHandler, XmlNode)

Source:
XmlDocument.cs
Source:
XmlDocument.cs
Source:
XmlDocument.cs

根據 Schemas 屬性中包含的 XML 結構描述定義語言 (XSD) 結構描述,驗證指定的 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

從要驗證的 XmlDocument 所建立的 XmlNode 物件。

例外狀況

XmlNode 物件參數不是從 XmlDocument 所建立。

XmlNode 物件參數不是項目、屬性、文件片段或根節點。

發生結構描述驗證事件且未指定任何 ValidationEventHandler 物件。

範例

如需 方法的 Validate 範例,請參閱 Validate 方法。

備註

方法 Validate 會根據 屬性中包含的 Schemas 架構,驗證 物件中的 XmlNode XML 資料。 方法 Validate 會執行資訊集擴增。 具體來說,成功驗證之後,會套用架構預設值、視需要將文字值轉換成不可部分完成的值,而且類型資訊會與已驗證的資訊專案相關聯。 結果是先前以具型別子樹取代的 XmlDocument 中未具型別的 XML 子樹狀結構。

以下是使用 Validate 方法時要考慮的重要注意事項。

  • xsi:noNamespaceSchemaLocation 之類的 xsi:schemaLocation 架構位置提示會被忽略。

  • 會忽略內嵌架構。

  • 如果在驗證期間發生架構驗證錯誤,則會 XmlDocument 部分使用具有正確型別資訊的節點進行部分驗證,有些則沒有 。

如果要驗證的節點是根節點,驗證程式會包含檢查唯一性和參考條件約束 (xs:IDxs:IDREF 、、 xs:keyxs:keyrefxs:unique) ,否則會省略唯一性和參考條件約束。

適用於