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 オブジェクトと XmlReader オブジェクトを使用して、関連付けられた XSD スキーマを含む をXmlReaderSettings作成します。 次に、 クラスを 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.xsd ファイルをcontosoBooks.xml入力として受け取ります。

<?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 、 プロパティに含まれるスキーマに XmlDocument 対して 内の XML データを Schemas 検証します。 メソッドは Validate 、インフォセット拡張を実行します。 具体的には、検証が成功すると、スキーマの既定値が適用され、テキスト値は必要に応じてアトミック値に変換され、型情報は検証済みの情報項目に関連付けられます。 結果は、 内の以前に型指定されていない XML サブツリー XmlDocument が型指定されたサブツリーに置き換えられます。

メソッドを使用する場合に考慮すべき重要な注意事項を次に Validate 示します。

  • xsi:noNamespaceSchemaLocation などのxsi:schemaLocationスキーマの場所ヒントは無視されます。

  • インライン スキーマは無視されます。

  • 検証中にスキーマ検証エラーが発生した場合、 XmlDocument は、正しい型情報を持ち、一部のノードではなしで部分的に検証されます。

  • 検証プロセスには、一意性と参照制約 (xs:IDxs:keyrefxs:IDREFxs:keyxs: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 、 プロパティに含まれるスキーマに XmlNode 対して オブジェクト内の XML データを Schemas 検証します。 メソッドは Validate 、インフォセット拡張を実行します。 具体的には、検証が成功すると、スキーマの既定値が適用され、テキスト値は必要に応じてアトミック値に変換され、型情報は検証済みの情報項目に関連付けられます。 結果は、 内の以前に型指定されていない XML サブツリー XmlDocument が型指定されたサブツリーに置き換えられます。

メソッドを使用する場合に考慮すべき重要な注意事項を次に Validate 示します。

  • xsi:noNamespaceSchemaLocation などのxsi:schemaLocationスキーマの場所ヒントは無視されます。

  • インライン スキーマは無視されます。

  • 検証中にスキーマ検証エラーが発生した場合、 XmlDocument は、正しい型情報を持ち、一部のノードではなしで部分的に検証されます。

検証するノードがルート ノードの場合、検証プロセスには一意性と参照制約 (xs:IDxs:keyrefxs:IDREFxs:keyおよび xs:unique) のチェックが含まれます。それ以外の場合は、一意性制約と参照制約が省略されます。

適用対象