XmlSchemaValidator のプッシュ ベースの検証

XmlSchemaValidator は、プッシュ ベース方式で、XML スキーマを基準として XML データを検証する、効率的かつ高性能なしくみを提供します。 たとえば、XmlSchemaValidator クラスでは、XML 情報セットを XML ドキュメントにシリアル化してから検証型 XML リーダーを使用してドキュメントを再解析する必要なしに、情報セットをそのまま検証することができます。

XmlSchemaValidator クラスは、カスタム XML データ ソースに対する検証エンジンの構築や検証型 XML ライターを構築する 1 つの手段とするなど、上級のシナリオで使用することができます。

次は、XmlSchemaValidator クラスを使用して、contosoBooks.xml ファイルを contosoBooks.xsd スキーマに対して検証する一例です。 この例では、XmlSerializer クラスを使用して、contosoBooks.xml ファイルを逆シリアル化し、ノードの値を XmlSchemaValidator クラスのメソッドに渡します。

注意

この例は、このトピックの各セクションを通じて使用されます。

using System;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using System.Collections;

namespace Microsoft.Samples.Xml.Schema
{
    class XmlSchemaValidatorExamples
    {
        static void Main()
        {
            // The XML document to deserialize into the XmlSerializer object.
            XmlReader reader = XmlReader.Create("contosoBooks.xml");

            // The XmlSerializer object.
            XmlSerializer serializer = new XmlSerializer(typeof(ContosoBooks));
            ContosoBooks books = (ContosoBooks)serializer.Deserialize(reader);

            // The XmlSchemaSet object containing the schema used to validate the XML document.
            XmlSchemaSet schemaSet = new XmlSchemaSet();
            schemaSet.Add("http://www.contoso.com/books", "contosoBooks.xsd");

            // The XmlNamespaceManager object used to handle namespaces.
            XmlNamespaceManager manager = new XmlNamespaceManager(reader.NameTable);

            // Assign a ValidationEventHandler to handle schema validation warnings and errors.
            XmlSchemaValidator validator = new XmlSchemaValidator(reader.NameTable, schemaSet, manager, XmlSchemaValidationFlags.None);
            validator.ValidationEventHandler += new ValidationEventHandler(SchemaValidationEventHandler);

            // Initialize the XmlSchemaValidator object.
            validator.Initialize();

            // Validate the bookstore element, verify that all required attributes are present
            // and prepare to validate child content.
            validator.ValidateElement("bookstore", "http://www.contoso.com/books", null);
            validator.GetUnspecifiedDefaultAttributes(new ArrayList());
            validator.ValidateEndOfAttributes(null);

            // Get the next expected element in the bookstore context.
            XmlSchemaParticle[] particles = validator.GetExpectedParticles();
            XmlSchemaElement nextElement = particles[0] as XmlSchemaElement;
            Console.WriteLine("Expected Element: '{0}'", nextElement.Name);

            foreach (BookType book in books.Book)
            {
                // Validate the book element.
                validator.ValidateElement("book", "http://www.contoso.com/books", null);

                // Get the expected attributes for the book element.
                Console.Write("\nExpected attributes: ");
                XmlSchemaAttribute[] attributes = validator.GetExpectedAttributes();
                foreach (XmlSchemaAttribute attribute in attributes)
                {
                    Console.Write("'{0}' ", attribute.Name);
                }
                Console.WriteLine();

                // Validate the genre attribute and display its post schema validation information.
                if (book.Genre != null)
                {
                    validator.ValidateAttribute("genre", "", book.Genre, schemaInfo);
                }
                DisplaySchemaInfo();

                // Validate the publicationdate attribute and display its post schema validation information.
                if (book.PublicationDate != null)
                {
                    validator.ValidateAttribute("publicationdate", "", dateTimeGetter(book.PublicationDate), schemaInfo);
                }
                DisplaySchemaInfo();

                // Validate the ISBN attribute and display its post schema validation information.
                if (book.Isbn != null)
                {
                    validator.ValidateAttribute("ISBN", "", book.Isbn, schemaInfo);
                }
                DisplaySchemaInfo();

                // After validating all the attributes for the current element with ValidateAttribute method,
                // you must call GetUnspecifiedDefaultAttributes to validate the default attributes.
                validator.GetUnspecifiedDefaultAttributes(new ArrayList());

                // Verify that all required attributes of the book element are present
                // and prepare to validate child content.
                validator.ValidateEndOfAttributes(null);

                // Validate the title element and its content.
                validator.ValidateElement("title", "http://www.contoso.com/books", null);
                validator.ValidateEndElement(null, book.Title);

                // Validate the author element, verify that all required attributes are present
                // and prepare to validate child content.
                validator.ValidateElement("author", "http://www.contoso.com/books", null);
                validator.GetUnspecifiedDefaultAttributes(new ArrayList());
                validator.ValidateEndOfAttributes(null);

                if (book.Author.Name != null)
                {
                    // Validate the name element and its content.
                    validator.ValidateElement("name", "http://www.contoso.com/books", null);
                    validator.ValidateEndElement(null, book.Author.Name);
                }

                if (book.Author.FirstName != null)
                {
                    // Validate the first-name element and its content.
                    validator.ValidateElement("first-name", "http://www.contoso.com/books", null);
                    validator.ValidateEndElement(null, book.Author.FirstName);
                }

                if (book.Author.LastName != null)
                {
                    // Validate the last-name element and its content.
                    validator.ValidateElement("last-name", "http://www.contoso.com/books", null);
                    validator.ValidateEndElement(null, book.Author.LastName);
                }

                // Validate the content of the author element.
                validator.ValidateEndElement(null);

                // Validate the price element and its content.
                validator.ValidateElement("price", "http://www.contoso.com/books", null);
                validator.ValidateEndElement(null, book.Price);

                // Validate the content of the book element.
                validator.ValidateEndElement(null);
            }

            // Validate the content of the bookstore element.
            validator.ValidateEndElement(null);

            // Close the XmlReader object.
            reader.Close();
        }

        static XmlSchemaInfo schemaInfo = new XmlSchemaInfo();
        static object dateTimeGetterContent;

        static object dateTimeGetterHandle()
        {
            return dateTimeGetterContent;
        }

        static XmlValueGetter dateTimeGetter(DateTime dateTime)
        {
            dateTimeGetterContent = dateTime;
            return new XmlValueGetter(dateTimeGetterHandle);
        }

        static void DisplaySchemaInfo()
        {
            if (schemaInfo.SchemaElement != null)
            {
                Console.WriteLine("Element '{0}' with type '{1}' is '{2}'",
                    schemaInfo.SchemaElement.Name, schemaInfo.SchemaType, schemaInfo.Validity);
            }
            else if (schemaInfo.SchemaAttribute != null)
            {
                Console.WriteLine("Attribute '{0}' with type '{1}' is '{2}'",
                    schemaInfo.SchemaAttribute.Name, schemaInfo.SchemaType, schemaInfo.Validity);
            }
        }

        static void SchemaValidationEventHandler(object sender, ValidationEventArgs e)
        {
            switch (e.Severity)
            {
                case XmlSeverityType.Error:
                    Console.WriteLine("\nError: {0}", e.Message);
                    break;
                case XmlSeverityType.Warning:
                    Console.WriteLine("\nWarning: {0}", e.Message);
                    break;
            }
        }
    }

    [XmlRootAttribute("bookstore", Namespace = "http://www.contoso.com/books", IsNullable = false)]
    public class ContosoBooks
    {
        [XmlElementAttribute("book")]
        public BookType[] Book;
    }

    public class BookType
    {
        [XmlAttributeAttribute("genre")]
        public string Genre;

        [XmlAttributeAttribute("publicationdate", DataType = "date")]
        public DateTime PublicationDate;

        [XmlAttributeAttribute("ISBN")]
        public string Isbn;

        [XmlElementAttribute("title")]
        public string Title;

        [XmlElementAttribute("author")]
        public BookAuthor Author;

        [XmlElementAttribute("price")]
        public Decimal Price;
    }

    public class BookAuthor
    {
        [XmlElementAttribute("name")]
        public string Name;

        [XmlElementAttribute("first-name")]
        public string FirstName;

        [XmlElementAttribute("last-name")]
        public string LastName;
    }
}
Imports System.Xml
Imports System.Xml.Schema
Imports System.Xml.Serialization
Imports System.Collections


Namespace Microsoft.Samples.Xml.Schema

    Class XmlSchemaValidatorExamples

        Shared Sub Main()

            ' The XML document to deserialize into the XmlSerializer object.
            Dim reader As XmlReader = XmlReader.Create("contosoBooks.xml")

            ' The XmlSerializer object.
            Dim serializer As XmlSerializer = New XmlSerializer(GetType(ContosoBooks))
            Dim books As ContosoBooks = CType(serializer.Deserialize(reader), ContosoBooks)

            ' The XmlSchemaSet object containing the schema used to validate the XML document.
            Dim schemaSet As XmlSchemaSet = New XmlSchemaSet()
            schemaSet.Add("http://www.contoso.com/books", "contosoBooks.xsd")

            ' The XmlNamespaceManager object used to handle namespaces.
            Dim manager As XmlNamespaceManager = New XmlNamespaceManager(reader.NameTable)

            ' Assign a ValidationEventHandler to handle schema validation warnings and errors.
            Dim validator As XmlSchemaValidator = New XmlSchemaValidator(reader.NameTable, schemaSet, manager, XmlSchemaValidationFlags.None)
            'validator.ValidationEventHandler += New ValidationEventHandler(SchemaValidationEventHandler)
            AddHandler validator.ValidationEventHandler, AddressOf SchemaValidationEventHandler

            ' Initialize the XmlSchemaValidator object.
            validator.Initialize()

            ' Validate the bookstore element, verify that all required attributes are present
            ' and prepare to validate child content.
            validator.ValidateElement("bookstore", "http://www.contoso.com/books", Nothing)

            validator.GetUnspecifiedDefaultAttributes(New ArrayList())
            validator.ValidateEndOfAttributes(Nothing)

            ' Get the next expected element in the bookstore context.
            Dim particles() As XmlSchemaParticle = validator.GetExpectedParticles()
            Dim nextElement As XmlSchemaElement = particles(0)
            Console.WriteLine("Expected Element: '{0}'", nextElement.Name)

            For Each book As BookType In books.book
                ' Validate the book element.
                validator.ValidateElement("book", "http://www.contoso.com/books", Nothing)

                ' Get the expected attributes for the book element.
                Console.Write(vbCrLf & "Expected attributes: ")
                Dim attributes() As XmlSchemaAttribute = validator.GetExpectedAttributes()
                For Each attribute As XmlSchemaAttribute In attributes
                    Console.Write("'{0}' ", attribute.Name)
                Next
                Console.WriteLine()

                ' Validate the genre attribute and display its post schema validation information.
                If Not book.Genre Is Nothing Then
                    validator.ValidateAttribute("genre", "", book.Genre, schemaInfo)
                End If
                DisplaySchemaInfo()

                ' Validate the publicationdate attribute and display its post schema validation information.
                If Not book.PublicationDate = Nothing Then
                    validator.ValidateAttribute("publicationdate", "", dateTimeGetter(book.PublicationDate), schemaInfo)
                End If
                DisplaySchemaInfo()

                ' Validate the ISBN attribute and display its post schema validation information.
                If Not book.Isbn Is Nothing Then
                    validator.ValidateAttribute("ISBN", "", book.Isbn, schemaInfo)
                End If
                DisplaySchemaInfo()

                ' After validating all the attributes for the current element with ValidateAttribute method,
                ' you must call GetUnspecifiedDefaultAttributes to validate the default attributes.
                validator.GetUnspecifiedDefaultAttributes(New ArrayList())

                ' Verify that all required attributes of the book element are present
                ' and prepare to validate child content.
                validator.ValidateEndOfAttributes(Nothing)

                ' Validate the title element and its content.
                validator.ValidateElement("title", "http://www.contoso.com/books", Nothing)
                validator.ValidateEndElement(Nothing, book.Title)

                ' Validate the author element, verify that all required attributes are present
                ' and prepare to validate child content.
                validator.ValidateElement("author", "http://www.contoso.com/books", Nothing)

                validator.GetUnspecifiedDefaultAttributes(New ArrayList())
                validator.ValidateEndOfAttributes(Nothing)

                If Not book.Author.Name Is Nothing Then
                    ' Validate the name element and its content.
                    validator.ValidateElement("name", "http://www.contoso.com/books", Nothing)
                    validator.ValidateEndElement(Nothing, book.Author.Name)
                End If

                If Not book.Author.FirstName Is Nothing Then
                    ' Validate the first-name element and its content.
                    validator.ValidateElement("first-name", "http://www.contoso.com/books", Nothing)
                    validator.ValidateEndElement(Nothing, book.Author.FirstName)

                End If

                If Not book.Author.LastName Is Nothing Then
                    ' Validate the last-name element and its content.
                    validator.ValidateElement("last-name", "http://www.contoso.com/books", Nothing)
                    validator.ValidateEndElement(Nothing, book.Author.LastName)
                End If

                ' Validate the content of the author element.
                validator.ValidateEndElement(Nothing)

                ' Validate the price element and its content.
                validator.ValidateElement("price", "http://www.contoso.com/books", Nothing)
                validator.ValidateEndElement(Nothing, book.Price)

                ' Validate the content of the book element.
                validator.ValidateEndElement(Nothing)
            Next

            ' Validate the content of the bookstore element.
            validator.ValidateEndElement(Nothing)

            ' Close the XmlReader object.
            reader.Close()

        End Sub

        Shared schemaInfo As XmlSchemaInfo = New XmlSchemaInfo()
        Shared dateTimeGetterContent As Object

        Shared Function dateTimeGetterHandle() As Object

            Return dateTimeGetterContent

        End Function

        Shared Function dateTimeGetter(ByVal dateTime As DateTime) As XmlValueGetter

            dateTimeGetterContent = dateTime
            Return New XmlValueGetter(AddressOf dateTimeGetterHandle)

        End Function

        Shared Sub DisplaySchemaInfo()

            If Not schemaInfo.SchemaElement Is Nothing Then
                Console.WriteLine("Element '{0}' with type '{1}' is '{2}'", schemaInfo.SchemaElement.Name, schemaInfo.SchemaType, schemaInfo.Validity)
            ElseIf Not schemaInfo.SchemaAttribute Is Nothing Then
                Console.WriteLine("Attribute '{0}' with type '{1}' is '{2}'", schemaInfo.SchemaAttribute.Name, schemaInfo.SchemaType, schemaInfo.Validity)
            End If

        End Sub

        Shared Sub SchemaValidationEventHandler(ByVal sender As Object, ByVal e As ValidationEventArgs)

            Select Case e.Severity
                Case XmlSeverityType.Error
                    Console.WriteLine(vbCrLf & "Error: {0}", e.Message)
                    Exit Sub
                Case XmlSeverityType.Warning
                    Console.WriteLine(vbCrLf & "Warning: {0}", e.Message)
                    Exit Sub
            End Select

        End Sub

    End Class

    <XmlRootAttribute("bookstore", Namespace:="http://www.contoso.com/books", IsNullable:=False)> _
    Public Class ContosoBooks

        <XmlElementAttribute("book")> _
        Public book() As BookType

    End Class

    Public Class BookType

        <XmlAttributeAttribute("genre")> _
        Public Genre As String

        <XmlAttributeAttribute("publicationdate", DataType:="date")> _
        Public PublicationDate As DateTime

        <XmlAttributeAttribute("ISBN")> _
        Public Isbn As String

        <XmlElementAttribute("title")> _
        Public Title As String

        <XmlElementAttribute("author")> _
        Public Author As BookAuthor

        <XmlElementAttribute("price")> _
        Public Price As Decimal

    End Class

    Public Class BookAuthor

        <XmlElementAttribute("name")> _
        Public Name As String

        <XmlElementAttribute("first-name")> _
        Public FirstName As String

        <XmlElementAttribute("last-name")> _
        Public LastName As String

    End Class

End Namespace

この例は、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>

また、contosoBooks.xsd ファイルも入力として使用します。

<?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>

XmlSchemaValidator を使用した XML データの検証

XML 情報セットの検証を開始するには、最初に XmlSchemaValidator コンストラクターを使用して XmlSchemaValidator クラスの新しいインスタンスを初期化する必要があります。

XmlSchemaValidator コンストラクターはパラメーターとして XmlNameTable 値の他に、XmlSchemaSetXmlNamespaceManager、および XmlSchemaValidationFlags オブジェクトをパラメーターとして取ります。 XmlNameTable オブジェクトは、スキーマの名前空間や XML 名前空間などの既知の名前空間文字列を分解するために使用され、単純コンテンツの検証中に、ParseValue メソッドに渡されます。 XmlSchemaSet オブジェクトには XML 情報セットを検証するために使用される XML スキーマが含まれています。 XmlNamespaceManager オブジェクトは検証中に遭遇する名前空間を解決するために使用されます。 XmlSchemaValidationFlags 値は、検証の特定の機能を無効にするために使用されます。

XmlSchemaValidator コンストラクターの詳細については、XmlSchemaValidator クラスのリファレンス ドキュメントを参照してください。

検証の初期化

XmlSchemaValidator オブジェクトの構築後に、Initialize オブジェクトの状態の初期化に使用される 2 つのオーバーロードされた XmlSchemaValidator メソッドがあります。 次が、その 2 つの Initialize メソッドです。

既定の XmlSchemaValidator.Initialize メソッドは XmlSchemaValidator オブジェクトを開始状態に初期化し、オーバーロードされた XmlSchemaValidator.Initialize メソッドは XmlSchemaObject をパラメーターとして取り、部分検証のために XmlSchemaValidator オブジェクトを開始状態に初期化します。

両方の Initialize メソッドは、XmlSchemaValidator オブジェクトが構築された直後、または EndValidation の呼び出し後のみ呼び出し可能です。

XmlSchemaValidator.Initialize メソッドの例については、最初の例を参照してください。 Initialize メソッドの詳細については、XmlSchemaValidator クラスのリファレンス ドキュメントを参照してください。

部分検証

XmlSchemaValidator.Initialize をパラメーターとして取る XmlSchemaObject メソッドは、部分検証のために XmlSchemaValidator オブジェクトを開始状態に初期化します。

次の例で XmlSchemaObject は、部分検証のために XmlSchemaValidator.Initialize メソッドを使用して初期化されます。 orderNumber オブジェクトの XmlQualifiedName プロパティによって返された XmlSchemaObjectTable コレクション内の GlobalElements によってスキーマ要素を選択することにより、XmlSchemaSet スキーマ要素が渡されます。 XmlSchemaValidator オブジェクトは、次にこの特定の要素を検証します。

Dim schemaSet As XmlSchemaSet = New XmlSchemaSet()
schemaSet.Add(Nothing, "schema.xsd")
schemaSet.Compile()
Dim nameTable As NameTable = New NameTable()
Dim manager As XmlNamespaceManager = New XmlNamespaceManager(nameTable)

Dim validator As XmlSchemaValidator = New XmlSchemaValidator(nameTable, schemaSet, manager, XmlSchemaValidationFlags.None)
validator.Initialize(schemaSet.GlobalElements.Item(New XmlQualifiedName("orderNumber")))

validator.ValidateElement("orderNumber", "", Nothing)
validator.ValidateEndOfAttributes(Nothing)
validator.ValidateText("123")
validator.ValidateEndElement(Nothing)
XmlSchemaSet schemaSet = new XmlSchemaSet();
schemaSet.Add(null, "schema.xsd");
schemaSet.Compile();
NameTable nameTable = new NameTable();
XmlNamespaceManager manager = new XmlNamespaceManager(nameTable);

XmlSchemaValidator validator = new XmlSchemaValidator(nameTable, schemaSet, manager, XmlSchemaValidationFlags.None);
validator.Initialize(schemaSet.GlobalElements[new XmlQualifiedName("orderNumber")]);

validator.ValidateElement("orderNumber", "", null);
validator.ValidateEndOfAttributes(null);
validator.ValidateText("123");
validator.ValidateEndElement(null);

この例は、次の XML スキーマを入力として使用します。

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="orderNumber" type="xs:int" />
</xs:schema>

Initialize メソッドの詳細については、XmlSchemaValidator クラスのリファレンス ドキュメントを参照してください。

追加のスキーマの追加

AddSchema クラスの XmlSchemaValidator メソッドは、検証中に使用するスキーマ セットに 1 つの XML スキーマを追加するために使用します。 AddSchema メソッドは、XML 情報セットの検証中に、インライン XML スキーマに遭遇した場合の効果をシミュレートするために使用できます。

注意

XmlSchema パラメーターの対象名前空間は、XmlSchemaValidator オブジェクトが既に遭遇したすべての要素と属性の名前空間と一致してはなりません。

XmlSchemaValidationFlags.ProcessInlineSchema コンストラクターに XmlSchemaValidator 値がパラメーターとして渡されなかった場合、AddSchema メソッドは何も行いません。

AddSchema メソッドの結果は、検証中の現在の XML ノードのコンテキストに依存します。 検証コンテキストの詳細については、このトピックの「検証コンテキスト」を参照してください。

AddSchema メソッドの詳細については、XmlSchemaValidator クラスのリファレンス ドキュメントを参照してください。

要素、属性、およびコンテンツの検証

XmlSchemaValidator クラスは、XML 情報セット中の要素、属性、およびコンテンツを XML スキーマに対して検証するために使用するいくつかのメソッドを提供します。 各メソッドに関する説明を次の表に示します。

メソッド 説明
ValidateElement 現在のコンテキスト中の要素名を検証します。
ValidateAttribute 現在の要素コンテキスト中の属性を検証します。または XmlSchemaAttribute メソッドにパラメーターとして渡された Initialize オブジェクトに対して属性を検証します。
ValidateEndOfAttributes 要素コンテキスト中の必須属性がすべて揃っていることを検証し、要素の子コンテンツを検証するために XmlSchemaValidator オブジェクトを準備します。
ValidateText 現在の要素コンテキスト中でテキストが許されるかどうかを検証し、現在の要素に単純コンテンツがある場合は検証のためにテキストを収集します。
ValidateWhitespace 現在の要素コンテキスト中で空白が許されるかどうかを検証し、現在の要素に単純コンテンツがある場合は検証のために空白を収集します。
ValidateEndElement 要素のテキスト コンテンツが、単純コンテンツの要素のデータ型に従って有効なことを検証し、現在の要素のコンテンツが複合コンテンツの要素として完全であることを検証します。
SkipToEndElement 現在の要素コンテンツの検証をスキップし、親要素のコンテキストでコンテンツの検証するために XmlSchemaValidator オブジェクトを準備します。
EndValidation 検証を終了し、ProcessIdentityConstraints 検証オプションが設定されている場合は XML ドキュメント全体について ID 制約をチェックします。

注意

XmlSchemaValidator クラスには、上の表で定義された各メソッド呼び出しの発生と順序を強制する、定義済みの状態遷移があります。 XmlSchemaValidator クラスの状態遷移の詳細は、このトピックの「XmlSchemaValidator の状態遷移」セクションで説明されています。

XML 情報セット内の要素、属性、およびコンテンツの検証に使用されるメソッドの例については、前のセクションの例を参照してください。 これらのメソッドの詳細については、XmlSchemaValidator クラスのリファレンス ドキュメントを参照してください。

XmlValueGetter を使用したコンテンツの検証

XmlValueGetterdelegate は、属性、テキスト、または空白のノードの値を、属性、テキスト、または空白ノードの XML スキーマ定義言語 (XSD) 型と互換性のある共通言語ランタイム (CLR) 型として渡すために使用できます。 XmlValueGetterdelegate は、属性、テキスト、または空白のノードの CLR 値が既に使用可能な場合、それを string に変換してから検証のために再度解析する手間を省くために役立ちます。

ValidateAttributeValidateText、および ValidateWhitespace メソッドはオーバーロードされ、属性、テキスト、または空白ノードの値を string または XmlValueGetterdelegate として受け入れます。

XmlSchemaValidator クラスの次のメソッドは、XmlValueGetterdelegate をパラメーターとして受け入れます。

次は、最初の XmlValueGetter クラスの例から取られた delegateXmlSchemaValidator の例です。 XmlValueGetterdelegate は属性の値を DateTime オブジェクトとして返します。 DateTime から返された XmlValueGetter オブジェクトを検証するために、XmlSchemaValidator オブジェクトは、最初にそれを属性のデータ型の ValueType (ValueType は XSD 型に対する既定の CLR の割り当て) に変換し、次に変換された値のファセットをチェックします。

Shared dateTimeGetterContent As Object

Shared Function DateTimeGetterHandle() As Object
    Return dateTimeGetterContent
End Function

Shared Function DateTimeGetter(dateTime As DateTime) As XmlValueGetter
    dateTimeGetterContent = dateTime
    Return New XmlValueGetter(AddressOf DateTimeGetterHandle)
End Function
static object dateTimeGetterContent;

static object DateTimeGetterHandle()
{
    return dateTimeGetterContent;
}

static XmlValueGetter DateTimeGetter(DateTime dateTime)
{
    dateTimeGetterContent = dateTime;
    return new XmlValueGetter(dateTimeGetterHandle);
}

XmlValueGetterdelegate の例については、最初の例を参照してください。 XmlValueGetterdelegate の詳細については、XmlValueGetter および XmlSchemaValidator クラスのリファレンス ドキュメントを参照してください。

スキーマの検証後の情報

XmlSchemaInfo クラスは XmlSchemaValidator クラスによって検証された 1 つの XML ノードのスキーマ検証後情報のいくつかを表します。 XmlSchemaValidator クラスの種々のメソッドは XmlSchemaInfo オブジェクトをオプションの (null) out パラメーターとして受け入れます。

検証が成功した後、XmlSchemaInfo オブジェクトには検証の結果が設定されています。 たとえば、ValidateAttribute メソッドを使用した検証の成功時には、XmlSchemaInfo オブジェクトの (指定した場合) SchemaAttributeSchemaTypeMemberType、および Validity プロパティに検証結果が設定されています。

次の XmlSchemaValidator クラスのメソッドは、XmlSchemaInfo オブジェクトを Out パラメーターとして受け入れます。

XmlSchemaInfo クラスの完全な例については、最初の例を参照してください。 XmlSchemaInfo クラスに関する詳細については、XmlSchemaInfo クラスのリファレンス ドキュメントを参照してください。

期待されるパーティクル、属性、および未指定の既定の属性の取得

XmlSchemaValidator クラスは、現在の検証コンテキスト中で期待されるパーティクル、属性、および未指定の既定の属性を取得するための GetExpectedAttributesGetExpectedParticles、および GetUnspecifiedDefaultAttributes メソッドを提供します。

期待されるパーティクルの取得

GetExpectedParticles メソッドは、現在の要素コンテキストで期待されるパーティクルを含む XmlSchemaParticle オブジェクトの配列を返します。 GetExpectedParticles メソッドが返せる有効なパーティクルは、XmlSchemaElement クラスと XmlSchemaAny クラスのインスタンスです。

コンテンツ モデルのコンポジターが xs:sequence の場合、シーケンス中の次のパーティクルのみが返されます。 コンテンツ モデルのコンポジターが xs:all または xs:choice の場合、現在の要素コンテキストに続くことができる有効なパーティクルすべてが返されます。

注意

GetExpectedParticles メソッドが Initialize メソッドの呼び出し直後に呼ばれると、GetExpectedParticles メソッドはすべてのグローバル要素を返します。

たとえば、XSD (XML スキーマ定義言語) スキーマと続く XML ドキュメントで、book 要素の検証後、book 要素は現在の要素コンテキストになります。 GetExpectedParticles メソッドは、XmlSchemaElement 要素を表す単一の title オブジェクトを含む配列を返します。 検証コンテキストが title 要素の場合、GetExpectedParticles メソッドは空の配列を返します。 GetExpectedParticles メソッドが title 要素の検証後で description 要素の検証前に呼び出されると、このメソッドは XmlSchemaElement 要素を表す単一の description オブジェクトを含む配列を返します。 GetExpectedParticles メソッドが description 要素の検証後に呼び出されると、メソッドはワイルドカードを表す単一の XmlSchemaAny オブジェクトを含む配列を返します。

Dim reader As XmlReader =  XmlReader.Create("input.xml")

Dim schemaSet As New XmlSchemaSet()
schemaSet.Add(Nothing, "schema.xsd")
Dim manager As New XmlNamespaceManager(reader.NameTable)

Dim validator As New XmlSchemaValidator(reader.NameTable,schemaSet,manager,XmlSchemaValidationFlags.None)
validator.Initialize()

validator.ValidateElement("book", "", Nothing)

validator.ValidateEndOfAttributes(Nothing)
For Each element As XmlSchemaElement In validator.GetExpectedParticles()
    Console.WriteLine(element.Name)
Next

validator.ValidateElement("title", "", Nothing)
validator.ValidateEndOfAttributes(Nothing)
For Each element As XmlSchemaElement In validator.GetExpectedParticles()
    Console.WriteLine(element.Name)
Next
validator.ValidateEndElement(Nothing)

For Each element As XmlSchemaElement In validator.GetExpectedParticles()
    Console.WriteLine(element.Name)
Next

validator.ValidateElement("description", "", Nothing)
validator.ValidateEndOfAttributes(Nothing)
validator.ValidateEndElement(Nothing)

For Each particle As XmlSchemaParticle In validator.GetExpectedParticles()
    Console.WriteLine(particle.GetType())
Next

validator.ValidateElement("namespace", "", Nothing)
validator.ValidateEndOfAttributes(Nothing)
validator.ValidateEndElement(Nothing)

validator.ValidateEndElement(Nothing)
XmlReader reader = XmlReader.Create("input.xml");

var schemaSet = new XmlSchemaSet();
schemaSet.Add(null, "schema.xsd");
var manager = new XmlNamespaceManager(reader.NameTable);

var validator = new XmlSchemaValidator(reader.NameTable, schemaSet, manager, XmlSchemaValidationFlags.None);
validator.Initialize();

validator.ValidateElement("book", "", null);

validator.ValidateEndOfAttributes(null);
foreach (XmlSchemaElement element in validator.GetExpectedParticles())
{
    Console.WriteLine(element.Name);
}

validator.ValidateElement("title", "", null);
validator.ValidateEndOfAttributes(null);
foreach (XmlSchemaElement element in validator.GetExpectedParticles())
{
    Console.WriteLine(element.Name);
}
validator.ValidateEndElement(null);

foreach (XmlSchemaElement element in validator.GetExpectedParticles())
{
    Console.WriteLine(element.Name);
}

validator.ValidateElement("description", "", null);
validator.ValidateEndOfAttributes(null);
validator.ValidateEndElement(null);

foreach (XmlSchemaParticle particle in validator.GetExpectedParticles())
{
    Console.WriteLine(particle.GetType());
}

validator.ValidateElement("namespace", "", null);
validator.ValidateEndOfAttributes(null);
validator.ValidateEndElement(null);

validator.ValidateEndElement(null);

この例は、次の XML を入力として使用します。

<xs:schema xmlns:xs="http://www.w3c.org/2001/XMLSchema">
  <xs:element name="book">
    <xs:sequence>
      <xs:element name="title" type="xs:string" />
      <xs:element name="description" type="xs:string" />
      <xs:any processContent="lax" maxOccurs="unbounded" />
    </xs:sequence>
  </xs:element>
</xs:schema>

この例は、次の XSD スキーマを入力として使用します。

<book>
  <title>My Book</title>
  <description>My Book's Description</description>
  <namespace>System.Xml.Schema</namespace>
</book>

注意

GetExpectedParticles クラスの GetExpectedAttributesAddSchema、および XmlSchemaValidator メソッドの結果は、検証中の現在のコンテキストに依存します。 詳細については、このトピックの「検証コンテキスト」を参照してください。

GetExpectedParticles メソッドの例については、最初の例を参照してください。 GetExpectedParticles メソッドの詳細については、XmlSchemaValidator クラスのリファレンス ドキュメントを参照してください。

期待される属性の取得

GetExpectedAttributes メソッドは、現在の要素コンテキストで期待される属性を含む XmlSchemaAttribute オブジェクトの配列を返します。

たとえば最初の例で、GetExpectedAttributes メソッドは book 要素のすべての属性を取得するために使用されます。

GetExpectedAttributes メソッドを ValidateElement メソッドの直後に呼び出すと、その XML ドキュメントで使用可能なすべての属性が返されます。 ただし、GetExpectedAttributes メソッドを ValidateAttribute メソッドの 1 回以上の呼び出し後に呼び出すと、現在の要素でまだ検証されていない属性が返されます。

注意

GetExpectedParticles クラスの GetExpectedAttributesAddSchema、および XmlSchemaValidator メソッドの結果は、検証中の現在のコンテキストに依存します。 詳細については、このトピックの「検証コンテキスト」を参照してください。

GetExpectedAttributes メソッドの例については、最初の例を参照してください。 GetExpectedAttributes メソッドの詳細については、XmlSchemaValidator クラスのリファレンス ドキュメントを参照してください。

未指定の既定の属性の取得

GetUnspecifiedDefaultAttributes メソッドは、要素コンテキスト中でこれまでに ArrayList メソッドを使用して検証されていない既定値を持つ属性の XmlSchemaAttribute オブジェクトを指定された ValidateAttribute に読み込みます。 GetUnspecifiedDefaultAttributes メソッドは、要素コンテキスト中の各属性について ValidateAttribute メソッドを呼び出した後で呼び出します。 GetUnspecifiedDefaultAttributes メソッドは、検証中の XML ドキュメントに挿入される既定の属性を決定するために使用します。

GetUnspecifiedDefaultAttributes メソッドの詳細については、XmlSchemaValidator クラスのリファレンス ドキュメントを参照してください。

スキーマ検証イベントの処理

検証中に発生したスキーマ検証の警告とエラーは、ValidationEventHandler クラスの XmlSchemaValidator イベントで取り扱われます。

スキーマ検証の警告は、XmlSeverityType 値が Warning であり、スキーマ検証エラーは、XmlSeverityType 値が Error です。 ValidationEventHandler が何も割り当てられていない場合、すべてのスキーマ検証エラーについて XmlSchemaValidationExceptionXmlSeverityTypeError でスローされます。 ただし、XmlSchemaValidationException 値が XmlSeverityType であるスキーマ検証警告については、Warning はスローされません。

次は最初の例から取られたもので、スキーマ検証中に発見したスキーマ検証警告とエラーを受け取る ValidationEventHandler の一例です。

Shared Sub SchemaValidationEventHandler(sender As Object, e As ValidationEventArgs)

    Select Case e.Severity
        Case XmlSeverityType.Error
            Console.WriteLine(vbCrLf & "Error: {0}", e.Message)
            Exit Sub
        Case XmlSeverityType.Warning
            Console.WriteLine(vbCrLf & "Warning: {0}", e.Message)
            Exit Sub
    End Select
End Sub
static void SchemaValidationEventHandler(object sender, ValidationEventArgs e)
{
    switch (e.Severity)
    {
        case XmlSeverityType.Error:
            Console.WriteLine("\nError: {0}", e.Message);
            break;
        case XmlSeverityType.Warning:
            Console.WriteLine("\nWarning: {0}", e.Message);
            break;
    }
}

ValidationEventHandler の完全な例については、最初の例を参照してください。 詳細については、XmlSchemaInfo クラスのリファレンス ドキュメントを参照してください。

XmlSchemaValidator の状態遷移

XmlSchemaValidator クラスには、XML 情報セット内の要素、属性、およびコンテンツを検証するために、各メソッド呼び出しの発生と順序を強制する定義済みの状態遷移があります。

次の表では、XmlSchemaValidator クラスの状態遷移と、各状態で可能なメソッド呼び出しとその順序について説明します。

状態 切り替え効果
検証 Initialize (ValidateAttribute | TopLevel*) EndValidation
TopLevel ValidateWhitespace | ValidateText | Element
要素 ValidateElementValidateAttribute* (ValidateEndOfAttributes Content*)? ValidateEndElement |

ValidateElement ValidateAttribute* SkipToEndElement |

ValidateElementValidateAttribute* ValidateEndOfAttributes Content* SkipToEndElement |
コンテンツ ValidateWhitespace | ValidateText | Element

注意

InvalidOperationException オブジェクトの現在の状態に従って、メソッドの呼び出しが正しい順序で行われないと、上表の各メソッドによって XmlSchemaValidator がスローされます。

上の状態遷移表では、XmlSchemaValidator クラスの状態遷移の各状態で呼び出せるメソッドと他の状態を説明するために、記号が使われています。 使用される記号は、ドキュメント型定義 (DTD) の XML 標準リファレンスで見られる記号と同一です。

次の表は、上の状態遷移表で見られる記号が XmlSchemaValidator クラスの状態遷移中の各状態について、呼び出せるメソッドと状態にどう影響を与えるかを説明するものです。

シンボル 説明
| メソッドと状態 (縦棒の前後の項目) のいずれかを呼び出し可能です。
? 疑問符の前のメソッドか状態はオプションです。呼び出す場合は 1 回しか呼べません。
* \* 記号の前のメソッドか状態はオプションです。複数回の呼び出しが可能です。

検証コンテキスト

XML 情報セット内の要素、属性、コンテンツの検証に使用される XmlSchemaValidator クラスのメソッドは、XmlSchemaValidator オブジェクトの検証コンテキストを変更します。 たとえば、SkipToEndElement メソッドは、現在の要素コンテンツの検証をスキップし、親要素のコンテキスト中のコンテンツを検証するために XmlSchemaValidator オブジェクトを準備します。これは、現在の要素のすべての子要素の検証をスキップして ValidateEndElement メソッドを呼ぶのと同じです。

GetExpectedParticles クラスの GetExpectedAttributesAddSchema、および XmlSchemaValidator メソッドの結果は、検証中の現在のコンテキストに依存します。

次の表では、XML 情報セット内の要素、属性、およびコンテンツの検証に使用する XmlSchemaValidator クラスのメソッドの 1 つを呼び出した後で、これらのメソッドを呼び出した場合の結果について説明します。

メソッド GetExpectedParticles GetExpectedAttributes AddSchema
Initialize 既定の Initialize メソッドが呼び出されると、GetExpectedParticles はすべてのグローバル要素を含む配列を返します。

1 つの要素の部分検証を初期化するために、Initialize をパラメーターとして取るオーバーロードされた XmlSchemaObject メソッドが呼び出された場合、GetExpectedParticles は、XmlSchemaValidator オブジェクトが初期化された対象の要素だけを返します。
既定の Initialize メソッドが呼び出されると、GetExpectedAttributes は空の配列を返します。

1 つの属性の部分検証を初期化するために Initialize をパラメーターとして取るオーバーロードの XmlSchemaObject メソッドが呼び出された場合、GetExpectedAttributes は、XmlSchemaValidator オブジェクトが初期化された対象の属性だけを返します。
プリプロセス エラーがない場合は、スキーマを XmlSchemaSet オブジェクトの XmlSchemaValidator に追加します。
ValidateElement コンテキスト要素が有効な場合、GetExpectedParticles はコンテキスト要素の子として期待される一連の要素を返します。

コンテキスト要素が無効な場合、GetExpectedParticles は空の配列を返します。
コンテキスト要素が有効で、これまで ValidateAttribute の呼び出しが行われていない場合、GetExpectedAttributes はコンテキスト要素上に定義されたすべての属性のリストを返します。

いくつかの属性が既に検証されている場合、GetExpectedAttributes は残りの検証すべき属性のリストを返します。

コンテキスト要素が無効な場合、GetExpectedAttributes は空の配列を返します。
上と同じ。
ValidateAttribute コンテキスト属性が最上位の属性である場合、GetExpectedParticles は空の配列を返します。

それ以外の場合、GetExpectedParticles は、コンテキスト要素の最初の子として期待される一連の要素を返します。
コンテキスト属性が最上位の属性である場合、GetExpectedAttributes は空の配列を返します。

それ以外の場合、GetExpectedAttributes は検証する残りの属性のリストを返します。
上と同じ。
GetUnspecifiedDefaultAttributes GetExpectedParticles は、コンテキスト要素の最初の子として期待される一連の要素を返します。 GetExpectedAttributes は、コンテキスト要素についてまだ検証されていない必須の属性とオプションの属性の一覧を返します。 上と同じ。
ValidateEndOfAttributes GetExpectedParticles は、コンテキスト要素の最初の子として期待される一連の要素を返します。 GetExpectedAttributes は空の配列を返します。 上と同じ。
ValidateText コンテキスト要素の contentType が Mixed の場合、GetExpectedParticles は、次の位置で期待される一連の要素を返します。

コンテキスト要素の contentType が TextOnly または Empty の場合、GetExpectedParticles は空の配列を返します。

コンテキスト要素の contentType が ElementOnly の場合、GetExpectedParticles は、検証エラーが既に発生しているものを除いて、次の位置で期待される一連の要素を返します。
GetExpectedAttributes はコンテキスト要素の検証されていない属性リストを返します。 上と同じ。
ValidateWhitespace コンテキストの空白が最上位の空白である場合、GetExpectedParticles は空の配列を返します。

それ以外の場合、GetExpectedParticles メソッドの動作は ValidateText の場合と同じです。
コンテキストの空白が最上位の空白である場合、GetExpectedAttributes は空の配列を返します。

それ以外の場合、GetExpectedAttributes メソッドの動作は ValidateText の場合と同じです。
上と同じ。
ValidateEndElement GetExpectedParticles はコンテキスト要素以降に期待される一連の要素を返します (兄弟の可能性)。 GetExpectedAttributes はコンテキスト要素の検証されていない属性リストを返します。

コンテキスト要素に親がない場合、GetExpectedAttributes は空のリストを返します (コンテキスト要素が、ValidateEndElement を呼び出した対象である現在の要素の親)。
上と同じ。
SkipToEndElement ValidateEndElement と同じ。 ValidateEndElement と同じ。 上と同じ。
EndValidation 空の配列を返します。 空の配列を返します。 上と同じ。

注意

XmlSchemaValidator クラスの各種プロパティで返される値は、上の表のどのメソッドを呼び出しても変わることはありません。

関連項目