XmlReaderSettings.ValidationType プロパティ

定義

XmlReader で読み込むときに検証または型の割り当てを実行するかどうかを示す値を取得または設定します。

C#
public System.Xml.ValidationType ValidationType { get; set; }

プロパティ値

ValidationType

読み込むときに XmlReader で検証または型の割り当てを実行するかどうかを示す ValidationType 値の 1 つ。 既定値は、ValidationType.None です。

次の例では、. に格納されているスキーマを使用して検証します XmlSchemaSet

C#
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'.

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

booksSchemaFail.xml

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

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

注釈

次の表では、値について ValidationType 説明します。

注意

列挙Auto値とXDR列挙値は、.NET Framework バージョン 2.0 では廃止されています。

[ValidationType] 説明
DTD 検証は、ドキュメント型定義 (DTD) を使用して実行されます。 メモ: また、このプロパティは DtdProcessing .Parse
None データを XmlReader 検証したり、型の割り当てを実行したりすることはありません。
Schema 検証と型の割り当ては、XML スキーマ定義言語 (XSD) スキーマを使用して実行されます。 リーダーは、次を使用して XML スキーマにアクセスします。

- プロパティを Schemas 使用して、このリーダーに XmlSchemaSet 関連付けられているオブジェクトにアクセスします。
- XML インスタンス ドキュメントに含まれるインライン スキーマを使用します。 (このオプションを ProcessInlineSchema 有効にする必要があります)。
- XML インスタンス ドキュメントで見つかったスキーマの場所ヒント (xsi:schemaLocation または xsi:noNamespaceSchemaLocation 属性) で指定された XML スキーマを使用します。 (このオプションを ProcessSchemaLocation 有効にする必要があります)。

適用対象

製品 バージョン
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 2.0, 2.1

こちらもご覧ください