XmlSchemaSet クラス
アセンブリ: System.Xml (system.xml.dll 内)


System.Xml Version 1.0 では、XML スキーマはスキーマのライブラリとして XmlSchemaCollection クラスに読み込まれました。System.Xml Version 2.0 では、XmlValidatingReader クラスと XmlSchemaCollection クラスは使用されなくなり、それぞれ Create メソッドと XmlSchemaSet クラスに置き換えられています。
XmlSchemaSet は、標準との互換性、パフォーマンス、および使用されなくなった XDR (XML-Data Reduced) スキーマ形式を含め、多くの問題を修正するために導入されました。
XmlSchemaCollection クラスと XmlSchemaSet クラスの比較を次に示します。
XmlSchemaCollection | XmlSchemaSet |
---|---|
Add メソッドを呼び出しても、スキーマはコンパイルされません。これにより、スキーマ ライブラリを作成しているときのパフォーマンスが向上します。 | |
それぞれのスキーマで個別のコンパイル済みバージョンが生成されるため、"孤立スキーマ" が形成されます。その結果、すべてのインクルードとインポートのスコープが、そのスキーマ内だけに設定されます。 | コンパイル済みスキーマにより、スキーマの "セット"である単一の論理スキーマが生成されます。このセットに組み込まれているスキーマ内にインポートされたスキーマは、そのセット自身に直接追加されます。これは、すべてのスキーマですべての型が使用できることを意味します。 |
セキュリティについての考慮事項
-
スキーマの要素をインクルード、インポート、および再定義するときに参照する外部の名前空間または外部の場所は、要素をインクルードまたはインポートしているスキーマのベース URI を基準にして解決されます。たとえば、インクルードまたはインポートしているスキーマのベース URI が空または null 参照 (Visual Basic では Nothing) の場合、外部の場所は現在のディレクトリを基準にして解決されます。XmlUrlResolver クラスは、外部のスキーマを解決するときに既定で使用されます。スキーマの要素のインクルード、インポート、および再定義における解決を無効にするには、XmlResolver プロパティを null 参照 (Visual Basic では Nothing) に設定します。
-
XmlSchemaSet クラスは、System.Text.RegularExpressions.Regex クラスを使用して XML スキーマ内の正規表現を解析し一致させます。XML スキーマ内の正規表現を使用してパターン ファセットを検証すると CPU の使用率が増加する場合があるため、高い可用性が求められる環境ではこの検証方法は避ける必要があります。
-
XmlSchemaSet クラスを使用した結果として発生した例外 (XmlSchemaException クラスなど) には、信頼関係のないシナリオで公開されてはならないような機密情報が含まれている場合があります。たとえば、XmlSchemaException の SourceUri プロパティは、例外が発生する原因となったスキーマ ファイルへの URI パスを返します。信頼関係のないシナリオでは、SourceUri プロパティを公開しないでください。こういった機密情報が信頼関係のないシナリオで公開されてしまわないよう、例外は適切に処理する必要があります。

XmlReaderSettings オブジェクトと System.Xml.XmlReader.Create メソッドを使用して、スキーマを XML ドキュメントに関連付ける例を次に示します。スキーマは、XmlReaderSettings オブジェクトの Schemas プロパティに追加されます。Schemas プロパティの値は XmlSchemaSet オブジェクトです。このスキーマは、XML ドキュメントがスキーマ コンテンツ モデルに準拠していることを検証するために使用されます。スキーマの検証エラーと検証警告は、XmlReaderSettings オブジェクトに定義された ValidationEventHandler で処理されます。
Imports System 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", "contosoBooks.xsd") booksSettings.ValidationType = ValidationType.Schema AddHandler booksSettings.ValidationEventHandler, New ValidationEventHandler(AddressOf booksSettingsValidationEventHandler) Dim books As XmlReader = XmlReader.Create("contosoBooks.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
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", "contosoBooks.xsd"); booksSettings.ValidationType = ValidationType.Schema; booksSettings.ValidationEventHandler += new ValidationEventHandler(booksSettingsValidationEventHandler); XmlReader books = XmlReader.Create("contosoBooks.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); } } }
#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; }
この例では、入力として 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>

System.Xml.Schema.XmlSchemaSet


Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- XmlSchemaSet クラスのページへのリンク