XmlSchemaValidator 푸시 기반 유효성 검사

XmlSchemaValidator 클래스는 밀어넣기 기반 방식으로 XML 스키마에 대해 XML 데이터의 유효성을 검사할 수 있는 효과적인 고성능 메커니즘을 제공합니다. 예를 들어, XmlSchemaValidator 클래스를 사용하면 XML 문서로 serialize한 다음 유효성 검사 XML 판독기를 사용하여 문서를 다시 구문 분석할 필요 없이 내부에서 직접 XML Infoset의 유효성을 검사할 수 있습니다.

사용자 지정 XML 데이터 소스에 대한 유효성 검사 엔진을 만드는 등의 고급 시나리오에서 또는 유효성 검사 XML 작성기를 만드는 방법으로 XmlSchemaValidator 클래스를 사용할 수 있습니다.

다음은 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 Infoset의 유효성 검사를 시작하려면 먼저 XmlSchemaValidator 생성자를 사용하여 XmlSchemaValidator 클래스의 새 인스턴스를 초기화해야 합니다.

XmlSchemaValidator 생성자는 XmlNameTable, XmlSchemaSet, XmlNamespaceManager 개체 및 XmlSchemaValidationFlags 값을 매개 변수로 사용합니다. XmlNameTable 개체는 스키마 네임스페이스, XML 네임스페이스 등의 잘 알려진 네임스페이스 문자열을 분해하는 데 사용되며 단순 내용의 유효성을 검사하는 동안 ParseValue 메서드에 전달됩니다. XmlSchemaSet 개체에는 XML Infoset의 유효성을 검사하는 데 사용되는 XML 스키마가 포함되어 있습니다. XmlNamespaceManager 개체를 사용하여 유효성 검사 중에 나타난 네임스페이스를 확인할 수 있습니다. XmlSchemaValidationFlags 값을 사용하여 특정 유효성 검사 기능을 비활성화할 수 있습니다.

XmlSchemaValidator 생성자에 대한 자세한 내용은 XmlSchemaValidator 클래스 참조 문서를 참조하세요.

유효성 검사 초기화

XmlSchemaValidator 개체를 생성한 후 Initialize 개체 상태를 초기화하는 데 사용하는 오버로드된 XmlSchemaValidator 메서드 두 개가 있습니다. 다음은 이 두 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 메서드를 사용하면 유효성 검사 중에 사용되는 스키마 집합에 XML 스키마를 추가할 수 있습니다. AddSchema 메서드를 사용하여 유효성을 검사 중인 XML Infoset에서 인라인 XML 스키마가 나타난 결과를 시뮬레이트할 수 있습니다.

참고 항목

XmlSchema 매개 변수의 대상 네임스페이스는 이미 XmlSchemaValidator 개체에 발생한 요소나 특성의 대상 네임스페이스와 달라야 합니다.

XmlSchemaValidationFlags.ProcessInlineSchema 값을 매개 변수로 XmlSchemaValidator 생성자에 전달하지 않은 경우 AddSchema 메서드는 아무 작업도 수행하지 않습니다.

AddSchema 메서드의 결과는 유효성을 검사 중인 현재 XML 노드 컨텍스트에 따라 다릅니다. 유효성 검사 컨텍스트에 대한 자세한 내용은 이 항목의 "유효성 검사 컨텍스트" 단원을 참조하세요.

AddSchema 메서드에 대한 자세한 내용은 XmlSchemaValidator 클래스 참조 문서를 참조하세요.

요소, 특성 및 내용 유효성 검사

XmlSchemaValidator 클래스는 XML Infoset에서 XML 스키마에 대해 요소, 특성 및 내용의 유효성을 검사할 수 있는 여러 메서드를 제공합니다. 다음 표에서는 이러한 메서드에 대해 설명합니다.

메서드 설명
ValidateElement 현재 컨텍스트에서 요소 이름의 유효성을 검사합니다.
ValidateAttribute 현재 요소 컨텍스트에서 또는 XmlSchemaAttribute 메서드에 매개 변수로 전달된 Initialize 개체에 대해 특성의 유효성을 검사합니다.
ValidateEndOfAttributes 요소 컨텍스트에 필수 특성이 모두 있는지 확인하고 XmlSchemaValidator 개체를 준비하여 요소의 자식 내용의 유효성을 검사합니다.
ValidateText 현재 요소 컨텍스트에서 텍스트가 허용되는지 확인하고 현재 요소에 단순 내용이 있는지 확인하기 위해 텍스트를 누적합니다.
ValidateWhitespace 현재 요소 컨텍스트에서 공백이 허용되는지 확인하고 현재 요소에 단순 내용이 있는지 확인하기 위해 공백을 누적합니다.
ValidateEndElement 단순 내용이 있는 요소의 데이터 형식에 따라 요소의 텍스트 내용이 유효한지 확인하고 현재 요소의 내용이 복합 내용이 있는 요소에 대해 완전한지 확인합니다.
SkipToEndElement 현재 요소 내용의 유효성 검사를 생략하고 XmlSchemaValidator 개체를 준비하여 부모 요소의 컨텍스트에서 내용의 유효성을 검사합니다.
EndValidation ProcessIdentityConstraints 유효성 검사 옵션을 설정한 경우 유효성 검사를 종료하고 전체 XML 문서에 대한 identity 제약 조건을 확인합니다.

참고 항목

XmlSchemaValidator 클래스에는 앞의 표에서 설명한 각 메서드의 호출 시퀀스를 실행하고 호출을 발생시키는 상태 전환이 정의되어 있습니다. XmlSchemaValidator 클래스의 특정 상태 전환에 대한 자세한 내용은 이 항목의 "XmlSchemaValidator 상태 전환" 단원을 참조하세요.

XML Infoset에서 요소, 특성 및 내용의 유효성을 검사하는 데 사용되는 메서드의 예는 이전 단원의 예제를 참조하세요. 이러한 메서드에 대한 자세한 내용은 XmlSchemaValidator 클래스 참조 문서를 참조하세요.

XmlValueGetter를 사용하여 내용 유효성 검사

XmlValueGetterdelegate를 사용하여 특성, 텍스트 또는 공백 노드의 값을 특성, 텍스트 또는 공백 노드의 XSD(XML 스키마 정의 언어) 형식과 호환되는 CLR(공용 언어 런타임) 형식으로 전달할 수 있습니다. 특성, 텍스트 또는 공백 노드의 CLR 값을 사용할 수 있는 경우 XmlValueGetterdelegate가 유용하며 string으로 변환하고 유효성을 검사하기 위해 다시 구문 분석하는 비용을 줄일 수 있습니다.

ValidateAttribute, ValidateTextValidateWhitespace 메서드가 오버로드되며 특성, 텍스트 또는 공백 노드의 값을 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에 대한 자세한 내용은 XmlValueGetterXmlSchemaValidator 클래스 참조 문서를 참조하세요.

Post-Schema-Validation-Information

XmlSchemaInfo 클래스는 XmlSchemaValidator 클래스로 유효성을 검사하는 XML 노드의 Post-Schema-Validation-Information을 나타냅니다. XmlSchemaValidator 클래스의 다양한 메서드에서는 XmlSchemaInfo 개체를 선택적(null) out 매개 변수로 허용합니다.

유효성 검사를 성공적으로 마치면 유효성 검사 결과와 함께 XmlSchemaInfo 개체가 설정됩니다. 예를 들어, ValidateAttribute 메서드를 사용하여 특성의 유효성을 검사하면 유효성 검사 결과와 함께 XmlSchemaInfo 개체(지정한 경우)의 SchemaAttribute, SchemaType, MemberTypeValidity 속성이 설정됩니다.

다음 XmlSchemaValidator 클래스 메서드에서는 XmlSchemaInfo 개체를 out 매개 변수로 허용합니다.

XmlSchemaInfo 클래스의 전체 예제는 소개 단원의 예제를 참조하세요. XmlSchemaInfo 클래스에 대한 자세한 내용은 XmlSchemaInfo 클래스 참조 문서를 참조하세요.

필요한 파티클, 특성 및 지정되지 않은 기본 특성 검색

XmlSchemaValidator 클래스는 현재 유효성 검사 컨텍스트에서 필요한 파티클, 특성 및 지정되지 않은 기본 특성을 검색하는 GetExpectedAttributes, GetExpectedParticlesGetUnspecifiedDefaultAttributes 메서드를 제공합니다.

필요한 파티클 검색

GetExpectedParticles 메서드는 현재 요소 컨텍스트에서 필요한 파티클이 포함된 XmlSchemaParticle 개체의 배열을 반환합니다. GetExpectedParticles 메서드가 반환할 수 있는 유효한 파티클은 XmlSchemaElementXmlSchemaAny 클래스의 인스턴스입니다.

내용 모델의 compositor가 xs:sequence인 경우 시퀀스에서 다음 파티클만 반환됩니다. 내용 모델의 compositor가 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 클래스의 GetExpectedAttributes, AddSchemaXmlSchemaValidator 메서드로 인한 결과는 유효성 검사가 실행 중인 현재 컨텍스트에 따라 달라집니다. 자세한 내용은 이 항목의 “유효성 검사 컨텍스트” 단원을 참조하세요.

GetExpectedParticles 메서드의 예제는 소개 단원의 예제를 참조하세요. GetExpectedParticles 메서드에 대한 자세한 내용은 XmlSchemaValidator 클래스 참조 문서를 참조하세요.

필요한 특성 검색

GetExpectedAttributes 메서드는 현재 요소 컨텍스트에서 필요한 특성이 포함된 XmlSchemaAttribute 개체의 배열을 반환합니다.

예를 들어, 소개 단원의 예제에서는 GetExpectedAttributes 메서드를 사용하여 book 요소의 모든 특성을 검색합니다.

GetExpectedAttributes 메서드를 호출한 직후에 ValidateElement 메서드를 호출하면 XML 문서에 나타날 수 있는 모든 특성이 반환됩니다. 그러나 GetExpectedAttributes 메서드를 한 번 이상 호출한 후 ValidateAttribute 메서드를 호출하면 현재 요소에 대해 아직 유효성을 검사하지 않은 특성이 반환됩니다.

참고 항목

GetExpectedParticles 클래스의 GetExpectedAttributes, AddSchemaXmlSchemaValidator 메서드로 인한 결과는 유효성 검사가 실행 중인 현재 컨텍스트에 따라 달라집니다. 자세한 내용은 이 항목의 “유효성 검사 컨텍스트” 단원을 참조하세요.

GetExpectedAttributes 메서드의 예제는 소개 단원의 예제를 참조하세요. GetExpectedAttributes 메서드에 대한 자세한 내용은 XmlSchemaValidator 클래스 참조 문서를 참조하세요.

지정되지 않은 기본 특성 검색

GetUnspecifiedDefaultAttributes 메서드는 요소 컨텍스트에서 ArrayList 메서드를 사용하여 기본값의 유효성을 검사하지 않은 특성의 XmlSchemaAttribute 개체로 지정한 ValidateAttribute를 채웁니다. 요소 컨텍스트의 각 특성에서 GetUnspecifiedDefaultAttributes 메서드를 호출한 후에 ValidateAttribute 메서드를 호출해야 합니다. 유효성을 검사 중인 XML 문서에 삽입할 기본 특성을 결정하려면 GetUnspecifiedDefaultAttributes 메서드를 사용해야 합니다.

GetUnspecifiedDefaultAttributes 메서드에 대한 자세한 내용은 XmlSchemaValidator 클래스 참조 문서를 참조하세요.

스키마 유효성 검사 이벤트 처리

유효성을 검사하는 동안 발생한 스키마 유효성 검사 경고 및 오류는 ValidationEventHandler 클래스의 XmlSchemaValidator 이벤트로 처리됩니다.

스키마 유효성 검사 경고의 XmlSeverityType 값은 Warning이며 스키마 유효성 검사 오류의 XmlSeverityType 값은 Error입니다. ValidationEventHandler를 지정하지 않으면 XmlSchemaValidationException 값이 XmlSeverityType인 모든 스키마 유효성 검사 오류에 대해 Error이 throw됩니다. 그러나 XmlSchemaValidationException 값이 XmlSeverityType인 스키마 유효성 검사 경고에 대해서는 Warning이 throw되지 않습니다.

다음은 소개 단원의 예제에서 가져온 것으로, 스키마 유효성 검사 중에 발생한 스키마 유효성 검사 경고 및 오류를 받는 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 Infoset에서 요소, 특성 및 내용의 유효성을 검사하는 데 사용되는 각 메서드의 호출 시퀀스를 실행하고 호출을 발생시키는 상태 전환이 정의되어 있습니다.

다음 표에서는 XmlSchemaValidator 클래스의 상태 전환 및 각 상태에서 수행할 수 있는 메서드 호출의 시퀀스와 발생에 대해 설명합니다.

State(상태) 전환
유효성 검사 Initialize(ValidateAttribute | TopLevel*) EndValidation
TopLevel ValidateWhitespace | ValidateText | 요소
요소 ValidateElementValidateAttribute*(ValidateEndOfAttributes 콘텐츠*)? ValidateEndElement |

ValidateElement ValidateAttribute* SkipToEndElement |

ValidateElementValidateAttribute* ValidateEndOfAttributes 콘텐츠* SkipToEndElement |
콘텐츠 ValidateWhitespace | ValidateText | 요소

참고 항목

위 표에서 InvalidOperationException 개체의 현재 상태에 따라 잘못된 시퀀스에서 메서드를 호출하면 각 메서드에 의해 XmlSchemaValidator이 throw됩니다.

위의 상태 전환 표에서는 문장 부호를 사용하여 XmlSchemaValidator 클래스 상태 전환의 각 상태에 대해 호출할 수 있는 메서드와 기타 상태를 설명합니다. 사용된 문장 부호는 DTD(문서 종류 정의)에 대한 XML 표준 참조의 문장 부호와 같습니다.

다음 표에서는 위의 상태 전환 표에 나타난 문장 부호가 XmlSchemaValidator 클래스 상태 전환의 각 상태에 대해 호출할 수 있는 메서드와 기타 상태에 미치는 영향을 설명합니다.

기호 설명
| 세로 막대 앞이나 뒤의 메서드 또는 상태를 호출할 수 있습니다.
? 물음표 앞의 메서드나 상태는 선택 항목이지만 이 메서드나 상태를 호출할 경우에는 한 번만 호출할 수 있습니다.
* * 기호 앞의 메서드나 상태는 선택 항목이며 두 번 이상 호출할 수 있습니다.

유효성 검사 컨텍스트

XML Infoset에서 요소, 특성 및 내용의 유효성을 검사하는 데 사용하는 XmlSchemaValidator 클래스의 메서드는 XmlSchemaValidator 개체의 유효성 검사 컨텍스트를 변경합니다. 예를 들어, SkipToEndElement 메서드는 현재 요소 내용의 유효성 검사를 생략하고 XmlSchemaValidator 개체를 준비하여 부모 요소의 컨텍스트에서 내용의 유효성을 검사합니다. 이 동작은 현재 요소의 모든 자식에 대한 유효성 검사를 생략하고 ValidateEndElement 메서드를 호출하는 것과 같습니다.

GetExpectedParticles 클래스의 GetExpectedAttributes, AddSchemaXmlSchemaValidator 메서드로 인한 결과는 유효성 검사가 실행 중인 현재 컨텍스트에 따라 달라집니다.

이 표에서는 XML Infoset에서 요소, 특성 및 내용의 유효성을 검사하는 데 사용하는 XmlSchemaValidator 클래스의 메서드 중 하나를 호출한 후 이러한 메서드를 호출한 결과를 설명합니다.

메서드 GetExpectedParticles GetExpectedAttributes AddSchema
Initialize 기본 Initialize 메서드를 호출할 경우 GetExpectedParticles는 모든 전역 요소가 포함된 배열을 반환합니다.

Initialize를 매개 변수로 사용하는 오버로드된 XmlSchemaObject 메서드를 호출하여 요소의 부분 유효성 검사를 초기화하는 경우 GetExpectedParticlesXmlSchemaValidator 개체가 초기화된 요소만 반환합니다.
기본 Initialize 메서드를 호출할 경우 GetExpectedAttributes는 빈 배열을 반환합니다.

Initialize를 매개 변수로 사용하는 오버로드된 XmlSchemaObject 메서드를 호출하여 특성의 부분 유효성 검사를 초기화하는 경우 GetExpectedAttributesXmlSchemaValidator 개체가 초기화된 특성만 반환합니다.
전처리 오류가 없을 경우 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 클래스의 다양한 속성이 반환하는 값은 위 표의 메서드를 호출해도 변경되지 않습니다.

참고 항목