XmlReaderSettings.Schemas 속성

정의

스키마 유효성 검사를 수행할 때 사용할 값을 가져오거나 설정합니다 XmlSchemaSet .

public:
 property System::Xml::Schema::XmlSchemaSet ^ Schemas { System::Xml::Schema::XmlSchemaSet ^ get(); void set(System::Xml::Schema::XmlSchemaSet ^ value); };
public System.Xml.Schema.XmlSchemaSet Schemas { get; set; }
member this.Schemas : System.Xml.Schema.XmlSchemaSet with get, set
Public Property Schemas As XmlSchemaSet

속성 값

XmlSchemaSet 스키마 유효성 검사를 수행할 때 사용할 값입니다. 기본값은 빈 XmlSchemaSet 개체입니다.

예제

아래 예제에서는 개체와 메서드를 XmlReaderSettings 사용하여 XmlReader.Create 스키마를 XML 문서와 연결합니다. 스키마가 개체의 속성에 SchemasXmlReaderSettings 추가됩니다. 속성 값이 Schemas 개체입니다 XmlSchemaSet . 스키마는 XML 문서가 스키마 콘텐츠 모델을 준수하는지 확인하는 데 사용됩니다. 스키마 유효성 검사 오류 및 경고는 개체에 ValidationEventHandler 정의된 것으로 XmlReaderSettings 처리됩니다.

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>

설명

이 문서는 Schemas 속성과 관련이 있습니다.

Important

  • 알 수 없거나 신뢰할 수 없는 원본 또는 위치의 스키마를 사용하지 마세요. 이렇게 하면 코드의 보안이 손상됩니다.
  • XML 스키마(인라인 스키마 포함)는 기본적으로 서비스 거부 공격에 취약합니다. 신뢰할 수 없는 시나리오에서는 허용하지 않습니다.
  • 스키마 유효성 검사 오류 메시지 및 예외는 콘텐츠 모델 또는 스키마 파일에 대한 URI 경로에 대한 중요한 정보를 노출할 수 있습니다. 신뢰할 수 없는 호출자에게 이 정보를 노출하지 않도록 주의하세요.
  • 자세한 내용은 "보안 고려 사항" 섹션을 참조하세요.

클래스는 XmlSchemaSet XSD(XML 스키마 정의 언어) 스키마만 지원합니다. XmlReader 메서드에서 만든 인스턴스는 Create XDR(XML-Data 축소) 스키마 유효성 검사를 사용하도록 구성할 수 없습니다.

보안 고려 사항

  • 알 수 없거나 신뢰할 수 없는 원본의 스키마를 사용하지 마세요. 이렇게 하면 코드의 보안이 손상됩니다. 클래스는 XmlUrlResolver 기본적으로 외부 스키마를 확인하는 데 사용됩니다. 스키마의 포함, 가져오기 및 다시 정의 요소의 확인을 사용하지 않도록 설정하려면 속성을 XmlSchemaSet.XmlResolver.로 설정합니다null.

  • 클래스를 사용한 XmlSchemaSet 결과로 발생한 예외(예: XmlSchemaException 클래스)는 신뢰할 수 없는 시나리오에서 노출되어서는 안 되는 중요한 정보를 포함할 수 있습니다. 예를 들어, SourceUriXmlSchemaException 속성은 예외를 발생시킨 스키마 파일의 URI 경로를 반환합니다. 신뢰할 수 없는 SourceUri 시나리오에서는 속성을 노출해서는 안 됩니다. 이 중요한 정보가 신뢰할 수 없는 시나리오에서 노출되지 않도록 예외를 올바르게 처리해야 합니다.

적용 대상

추가 정보