XmlReaderSettings.Schemas 屬性

定義

取得或設定要在執行結構描述驗證時使用的 XmlSchemaSetGets or sets the XmlSchemaSet to use when performing schema validation.

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

屬性值

執行結構描述驗證時使用的XmlSchemaSetThe XmlSchemaSet to use when performing schema validation. 預設為空的 XmlSchemaSet 物件。The default is an empty XmlSchemaSet object.

範例

下列範例會使用 XmlReaderSettings 物件和 XmlReader.Create 方法,將架構與 XML 檔產生關聯。The example below uses the XmlReaderSettings object and the XmlReader.Create method to associate a schema with an XML document. 架構會加入至 XmlReaderSettings 物件的 Schemas 屬性。The schema is added to the Schemas property of the XmlReaderSettings object. Schemas 屬性的值是 XmlSchemaSet 物件。The value of the Schemas property is an XmlSchemaSet object. 架構是用來驗證 XML 檔是否符合架構內容模型。The schema is used to validate that the XML document conforms to the schema content model. 架構驗證錯誤和警告是由 XmlReaderSettings 物件中定義的 ValidationEventHandler 所處理。Schema validation errors and warnings are handled by the ValidationEventHandler defined in the XmlReaderSettings object.

#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 檔案作為輸入。The example uses the books.xml file as input.

<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.xml 檔做為輸入。The example uses the books.xsd file as an input.

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

備註

重要

  • 請勿使用來自未知或不受信任來源或位置的架構。Do not use schemas from unknown or untrusted sources or locations. 這麼做會危害程式碼的安全性。Doing so will compromise the security of your code.
  • XML 架構(包括內嵌架構)原本就很容易遭到阻絕服務攻擊;請勿在未受信任的案例中接受這些條款。XML schemas (including inline schemas) are inherently vulnerable to denial of service attacks; do not accept them in untrusted scenarios.
  • 架構驗證錯誤訊息和例外狀況可能會將有關內容模型或 URI 路徑的機密資訊公開給架構檔案。Schema validation error messages and exceptions may expose sensitive information about the content model or URI paths to the schema file. 請小心不要向不受信任的呼叫者公開此資訊。Be careful not to expose this information to untrusted callers.
  • 如需詳細資訊,請參閱「安全性考慮」一節。For additional information, see the "Security considerations" section.

XmlSchemaSet 類別僅支援 XML 架構定義語言(XSD)架構。The XmlSchemaSet class only supports XML Schema definition language (XSD) schemas. Create 方法所建立的 XmlReader 實例無法設定為啟用 XML 資料精簡(XDR)架構驗證。XmlReader instances created by the Create method cannot be configured to enable XML-Data Reduced (XDR) schema validation.

安全性考量Security considerations

  • 請勿使用來自未知或不受信任來源的架構。Do not use schemas from unknown or untrusted sources. 這麼做會危害程式碼的安全性。Doing so will compromise the security of your code. 預設會使用 XmlUrlResolver 類別來解析外部架構。The XmlUrlResolver class is used to resolve external schemas by default. 若要停用架構之 include、import 和 redefine 元素的解析,請將 XmlSchemaSet.XmlResolver 屬性設為 nullTo disable resolution of include, import, and redefine elements of a schema, set the XmlSchemaSet.XmlResolver property to null.

  • 因使用 XmlSchemaSet 類別而引發的例外狀況,例如 XmlSchemaException 類別可能包含不應該在未受信任的案例中公開的機密資訊。Exceptions raised as a result of using the XmlSchemaSet class, such as the XmlSchemaException class may contain sensitive information that should not be exposed in untrusted scenarios. 例如,XmlSchemaExceptionSourceUri 屬性會傳回造成例外狀況之架構檔案的 URI 路徑。For example, the SourceUri property of an XmlSchemaException returns the URI path to the schema file that caused the exception. 不應在未受信任的案例中公開 SourceUri 屬性。The SourceUri property should not be exposed in untrusted scenarios. 應該適當地處理例外狀況,如此一來,就不會在未受信任的案例中公開此機密資訊。Exceptions should be properly handled so that this sensitive information is not exposed in untrusted scenarios.

適用於

另請參閱