次の方法で共有


System.Xml.Schema.XmlSchemaSet クラス

この記事では、この API のリファレンス ドキュメントに補足的な解説を提供します。

Von Bedeutung

  • 不明または信頼されていないソースまたは場所のスキーマは使用しないでください。 これにより、コードのセキュリティが損なわれます。
  • XML スキーマ (インライン スキーマを含む) は、本質的にサービス拒否攻撃に対して脆弱です。信頼されていないシナリオでは受け入れないでください。
  • スキーマ検証エラー メッセージと例外により、コンテンツ モデルまたは URI パスに関する機密情報がスキーマ ファイルに公開される場合があります。 信頼されていない呼び出し元にこの情報を公開しないように注意してください。
  • セキュリティに関するその他の考慮事項については、「セキュリティに関する考慮事項」セクションを参照してください。

XmlSchemaSet は、XML スキーマ定義言語 (XSD) スキーマを格納できるキャッシュまたはライブラリです。 XmlSchemaSet では、ファイルまたは URL からスキーマにアクセスするのではなく、メモリ内にスキーマをキャッシュすることでパフォーマンスが向上します。 各スキーマは、スキーマがセットに追加されたときに指定された名前空間 URI と場所によって識別されます。 XmlReaderSettings.Schemas プロパティを使用して、XML リーダーがデータ検証に使用するXmlSchemaSet オブジェクトを割り当てます。

セキュリティに関する考慮事項

  • 不明なソースまたは信頼されていないソースのスキーマは使用しないでください。 これにより、コードのセキュリティが損なわれます。 参照される外部名前空間または場所には、スキーマの要素を含める、インポートする、再定義する要素が含まれるかインポートされるスキーマのベース URI に関して解決されます。 たとえば、インクルードまたはインポートするスキーマのベース URI が空または null場合、外部の場所は現在のディレクトリに関して解決されます。 XmlUrlResolver クラスは、既定で外部スキーマを解決するために使用されます。 スキーマの要素のインクルード、インポート、再定義の解決を無効にするには、 XmlSchemaSet.XmlResolver プロパティを null に設定します。

  • XmlSchemaSet クラスは、System.Text.RegularExpressions.Regex クラスを使用して、XML スキーマ内の正規表現を解析して照合します。 XML スキーマの正規表現を使用したパターン ファセットの検証には CPU 使用率の増加が伴う場合があり、高可用性シナリオでは避ける必要があります。

  • XmlSchemaException クラスなど、XmlSchemaSet クラスを使用した結果として発生する例外には、信頼されていないシナリオでは公開すべきでない機密情報が含まれている場合があります。 たとえば、XmlSchemaExceptionSourceUri プロパティは、例外の原因となったスキーマ ファイルへの URI パスを返します。 信頼されていないシナリオでは、 SourceUri プロパティを公開しないでください。 信頼されていないシナリオでこの機密情報が公開されないように、例外を適切に処理する必要があります。

例示

次の例では、 XmlSchemaSetに格納されているスキーマを使用して XML ファイルを検証します。 XML ファイル内の名前空間は、検証に使用する内のスキーマを識別します。 この例からの出力は、XML ファイルに次の 2 つのスキーマ違反があることを示しています。

  • 最初の <book> 要素には <author> 要素が含まれていますが、 <title> または <price> 要素は含まれます。

  • 最後の<book>要素の<author>要素に<名>および<last-name>要素がないため、代わりに無効な<name>要素があります。

using System;
using System.Xml;
using System.Xml.Schema;
using System.IO;

public class Sample
{
  public static void Main() {

    // Create the XmlSchemaSet class.
    XmlSchemaSet sc = new XmlSchemaSet();

    // Add the schema to the collection.
    sc.Add("urn:bookstore-schema", "books.xsd");

    // Set the validation settings.
    XmlReaderSettings settings = new XmlReaderSettings();
    settings.ValidationType = ValidationType.Schema;
    settings.Schemas = sc;
    settings.ValidationEventHandler += ValidationCallBack;

    // Create the XmlReader object.
    XmlReader reader = XmlReader.Create("booksSchemaFail.xml", settings);

    // Parse the file.
    while (reader.Read());
  }

  // Display any validation errors.
  private static void ValidationCallBack(object sender, ValidationEventArgs e) {
    Console.WriteLine($"Validation Error:\n   {e.Message}\n");
  }
}
// The example displays output like the following:
//   Validation Error:
//        The element 'book' in namespace 'urn:bookstore-schema' has invalid child element 'author'
//        in namespace 'urn:bookstore-schema'. List of possible elements expected: 'title' in
//        namespace 'urn:bookstore-schema'.
//
//    Validation Error:
//       The element 'author' in namespace 'urn:bookstore-schema' has invalid child element 'name'
//       in namespace 'urn:bookstore-schema'. List of possible elements expected: 'first-name' in
//       namespace 'urn:bookstore-schema'.
Imports System.Xml
Imports System.Xml.Schema
Imports System.IO

Public Module Sample 
  Public Sub Main() 

    ' Create the XmlSchemaSet class.
    Dim sc as XmlSchemaSet = new XmlSchemaSet()

    ' Add the schema to the collection.
    sc.Add("urn:bookstore-schema", "books.xsd")

    ' Set the validation settings.
    Dim settings as XmlReaderSettings = new XmlReaderSettings()
    settings.ValidationType = ValidationType.Schema
    settings.Schemas = sc
    AddHandler settings.ValidationEventHandler, AddressOf ValidationCallBack
 
    ' Create the XmlReader object.
    Dim reader as XmlReader = XmlReader.Create("booksSchemaFail.xml", settings)

    ' Parse the file. 
    While reader.Read()
    End While
    
  End Sub

  ' Display any validation errors.
  Private Sub ValidationCallBack(sender as object, e as ValidationEventArgs) 
    Console.WriteLine($"Validation Error:{vbCrLf}   {e.Message}")
    Console.WriteLine()
  End Sub
End Module
' The example displays output like the following:
'   Validation Error: 
'        The element 'book' in namespace 'urn:bookstore-schema' has invalid child element 'author' 
'        in namespace 'urn:bookstore-schema'. List of possible elements expected: 'title' in 
'        namespace 'urn:bookstore-schema'.
'
'    Validation Error: 
'       The element 'author' in namespace 'urn:bookstore-schema' has invalid child element 'name' 
'       in namespace 'urn:bookstore-schema'. List of possible elements expected: 'first-name' in 
'       namespace 'urn:bookstore-schema'.

インプット

このサンプルでは、次の 2 つの入力ファイルを使用します。

booksSchemaFail.xml:

<?xml version='1.0'?>
<bookstore xmlns="urn:bookstore-schema">
  <book>
    <author>
      <first-name>Benjamin</first-name>
      <last-name>Franklin</last-name>
    </author>
  </book>
  <book genre="novel">
    <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">
    <title>The Gorgias</title>
    <author>
      <name>Plato</name>
    </author>
    <price>9.99</price>
  </book>
</bookstore>

books.xsd:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns="urn:bookstore-schema"
    elementFormDefault="qualified"
    targetNamespace="urn:bookstore-schema">

 <xsd:element name="bookstore" type="bookstoreType"/>

 <xsd:complexType name="bookstoreType">
  <xsd:sequence maxOccurs="unbounded">
   <xsd:element name="book"  type="bookType"/>
  </xsd:sequence>
 </xsd:complexType>

 <xsd:complexType name="bookType">
  <xsd:sequence>
   <xsd:element name="title" type="xsd:string"/>
   <xsd:element name="author" type="authorName"/>
   <xsd:element name="price"  type="xsd:decimal"/>
  </xsd:sequence>
  <xsd:attribute name="genre" type="xsd:string"/>
 </xsd:complexType>

 <xsd:complexType name="authorName">
  <xsd:sequence>
   <xsd:element name="first-name"  type="xsd:string"/>
   <xsd:element name="last-name" type="xsd:string"/>
  </xsd:sequence>
 </xsd:complexType>

</xsd:schema>