XmlSchemaSet.ValidationEventHandler 이벤트

정의

XSD(XML 스키마 정의 언어) 스키마 유효성 검사 오류에 대한 정보를 받을 이벤트 처리기를 지정합니다.

public:
 event System::Xml::Schema::ValidationEventHandler ^ ValidationEventHandler;
public event System.Xml.Schema.ValidationEventHandler ValidationEventHandler;
member this.ValidationEventHandler : System.Xml.Schema.ValidationEventHandler 

이벤트 유형

예제

다음 코드 예제에서는 스키마에 대해 XML 문서의 유효성을 검사할 때 오류 및 경고를 캡처하기 위해 를 추가하는 ValidationEventHandler 방법을 보여 줍니다.

#using <System.Xml.dll>

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

static void booksSettingsValidationEventHandler( Object^ /*sender*/, ValidationEventArgs^ e )
{
   if ( e->Severity == XmlSeverityType::Warning )
   {
      Console::Write( L"WARNING: " );
      Console::WriteLine( e->Message );
   }
   else
   if ( e->Severity == XmlSeverityType::Error )
   {
      Console::Write( L"ERROR: " );
      Console::WriteLine( e->Message );
   }
}

int main()
{
   XmlReaderSettings^ booksSettings = gcnew XmlReaderSettings;
   booksSettings->Schemas->Add( L"http://www.contoso.com/books", L"books.xsd" );
   booksSettings->ValidationType = ValidationType::Schema;
   booksSettings->ValidationEventHandler += gcnew ValidationEventHandler( booksSettingsValidationEventHandler );
   XmlReader^ books = XmlReader::Create( L"books.xml", booksSettings );
   while ( books->Read() )
   {}

   return 0;
}
using System;
using System.Xml;
using System.Xml.Schema;

class XmlSchemaSetExample
{
    static void Main()
    {
        XmlReaderSettings booksSettings = new XmlReaderSettings();
        booksSettings.Schemas.Add("http://www.contoso.com/books", "books.xsd");
        booksSettings.ValidationType = ValidationType.Schema;
        booksSettings.ValidationEventHandler += new ValidationEventHandler(booksSettingsValidationEventHandler);

        XmlReader books = XmlReader.Create("books.xml", booksSettings);

        while (books.Read()) { }
    }

    static void booksSettingsValidationEventHandler(object sender, ValidationEventArgs e)
    {
        if (e.Severity == XmlSeverityType.Warning)
        {
            Console.Write("WARNING: ");
            Console.WriteLine(e.Message);
        }
        else if (e.Severity == XmlSeverityType.Error)
        {
            Console.Write("ERROR: ");
            Console.WriteLine(e.Message);
        }
    }
}
Imports System.Xml
Imports System.Xml.Schema

Class XmlSchemaSetExample

    Shared Sub Main()

        Dim booksSettings As XmlReaderSettings = New XmlReaderSettings()
        booksSettings.Schemas.Add("http://www.contoso.com/books", "books.xsd")
        booksSettings.ValidationType = ValidationType.Schema
        AddHandler booksSettings.ValidationEventHandler, New ValidationEventHandler(AddressOf booksSettingsValidationEventHandler)

        Dim books As XmlReader = XmlReader.Create("books.xml", booksSettings)

        While books.Read()

        End While

    End Sub

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

        If e.Severity = XmlSeverityType.Warning Then
            Console.Write("WARNING: ")
            Console.WriteLine(e.Message)

        ElseIf e.Severity = XmlSeverityType.Error Then
            Console.Write("ERROR: ")
            Console.WriteLine(e.Message)
        End If

    End Sub

End Class

이 예제에서는 books.xml 파일을 입력으로 사용합니다.

<bookstore xmlns="http://www.contoso.com/books">
  <book genre="autobiography" publicationdate="1981" 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" 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" ISBN="1-861001-57-6">
    <title>The Gorgias</title>
    <author>
      <name>Plato</name>
    </author>
    <price>9.99</price>
  </book>
</bookstore>

이 예제에서는 books.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:unsignedShort" use="required" />
                        <xs:attribute name="ISBN" type="xs:string" use="required" />
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

설명

의 또는 Compile 메서드 XmlSchemaSet 가 호출되면 스키마 유효성 검사 오류 Add 에 대한 정보를 수신하기 위한 이벤트 처리기를 설정합니다.

이벤트 처리기가 정의 XmlSchemaException 되지 않은 경우 가 인 유효성 검사 오류에서 이 XmlSeverityType throw됩니다 Error. 의 유효성 검사 오류에 대해 예외가 XmlSeverityTypeWarningthrow되지 않습니다.

적용 대상