指定した XmlReader から返される内容を検証する XmlValidatingReader クラスの新しいインスタンスを初期化します。
名前空間: System.Xml
アセンブリ: System.Xml (system.xml.dll 内)
構文
'宣言
Public Sub New ( _
reader As XmlReader _
)
'使用
Dim reader As XmlReader
Dim instance As New XmlValidatingReader(reader)
public XmlValidatingReader (
XmlReader reader
)
public:
XmlValidatingReader (
XmlReader^ reader
)
public XmlValidatingReader (
XmlReader reader
)
public function XmlValidatingReader (
reader : XmlReader
)
パラメータ
- reader
検証中に読み取る対象の XmlReader。現在の実装では、XmlTextReader のみサポートします。
例外
| 例外の種類 | 条件 |
|---|---|
指定したリーダーが XmlTextReader ではありません。 |
解説
注意
Microsoft .NET Framework version 2.0 では、XmlValidatingReader クラスは使用されなくなりました。検証を実行する XmlReader のインスタンスは、XmlReaderSettings クラスおよび Create メソッドを使用して作成できます。詳細については、「XmlReader による XML データの検証」を参照してください。
指定した XmlReader から返されるノードはすべて、検証を実行するこのリーダーからも返されるため、プロセスでの情報の消失がありません。基になるリーダーから返されない新しいノード (既定の属性やエンティティ参照の子など) は、このリーダーで追加できます。指定した XmlTextReader のプロパティ セットもこの検証リーダーに適用されます。たとえば、提供されたリーダーに WhitespaceHandling.None セットが含まれている場合、この検証リーダーも空白を無視します。
検証で外部ドキュメント型定義 (DTD) またはスキーマが必要な場合、XmlResolver プロパティは、外部リソースを解決する XmlResolver オブジェクトを設定します。
使用例
2 つのドキュメントを検証する例を次に示します。
Imports System
Imports System.IO
Imports System.Xml
Imports System.Xml.Schema
Imports Microsoft.VisualBasic
public class Sample
private m_success as Boolean = true
public sub New ()
'Validate the document using an external XSD schema. Validation should fail.
Validate("notValidXSD.xml")
'Validate the document using an inline XSD. Validation should succeed.
Validate("inlineXSD.xml")
end sub
public shared sub Main ()
Dim validation as Sample = new Sample()
end sub
private sub Validate(filename as String)
m_success = true
Console.WriteLine()
Console.WriteLine("******")
Console.WriteLine("Validating XML file " + filename.ToString())
Dim txtreader as XmlTextReader = new XmlTextReader (filename)
Dim reader as XmlValidatingReader = new XmlValidatingReader (txtreader)
' Set the validation event handler
AddHandler reader.ValidationEventHandler, AddressOf ValidationCallBack
' Read XML data
while (reader.Read())
end while
Console.WriteLine ("Validation finished. Validation {0}", IIf(m_success, "successful!", "failed."))
'Close the reader.
reader.Close()
end sub
'Display the validation error.
Private sub ValidationCallBack (sender as object, args as ValidationEventArgs)
m_success = false
Console.WriteLine()
Console.WriteLine(" Validation error: " + args.Message )
end sub
end class
using System;
using System.IO;
using System.Xml;
using System.Xml.Schema;
public class Sample
{
private Boolean m_success = true;
public Sample ()
{
//Validate the document using an external XSD schema. Validation should fail.
Validate("notValidXSD.xml");
//Validate the document using an inline XSD. Validation should succeed.
Validate("inlineXSD.xml");
}
public static void Main ()
{
Sample validation = new Sample();
}
private void Validate(String filename)
{
m_success = true;
Console.WriteLine("\r\n******");
Console.WriteLine("Validating XML file " + filename.ToString());
XmlTextReader txtreader = new XmlTextReader (filename);
XmlValidatingReader reader = new XmlValidatingReader (txtreader);
// Set the validation event handler
reader.ValidationEventHandler += new ValidationEventHandler (ValidationCallBack);
// Read XML data
while (reader.Read()){}
Console.WriteLine ("Validation finished. Validation {0}", (m_success==true ? "successful!" : "failed."));
//Close the reader.
reader.Close();
}
//Display the validation error.
private void ValidationCallBack (object sender, ValidationEventArgs args)
{
m_success = false;
Console.WriteLine("\r\n\tValidation error: " + args.Message );
}
}
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Schema;
public ref class Sample
{
private:
static Boolean m_success = true;
public:
Sample()
{
// Validate the document using an external XSD schema. Validation should fail.
Validate( "notValidXSD.xml" );
// Validate the document using an inline XSD. Validation should succeed.
Validate( "inlineXSD.xml" );
}
private:
// Display the validation error.
void ValidationCallBack( Object^ /*sender*/, ValidationEventArgs^ args )
{
m_success = false;
Console::WriteLine( "\r\n\tValidation error: {0}", args->Message );
}
void Validate( String^ filename )
{
m_success = true;
Console::WriteLine( "\r\n******" );
Console::WriteLine( "Validating XML file {0}", filename );
XmlTextReader^ txtreader = gcnew XmlTextReader( filename );
XmlValidatingReader^ reader = gcnew XmlValidatingReader( txtreader );
// Set the validation event handler
reader->ValidationEventHandler += gcnew ValidationEventHandler( this, &Sample::ValidationCallBack );
// Read XML data
while ( reader->Read() )
{}
Console::WriteLine( "Validation finished. Validation {0}", (m_success == true ? (String^)"successful!" : "failed.") );
// Close the reader.
reader->Close();
}
};
int main()
{
Sample^ validation = gcnew Sample;
}
import System.*;
import System.IO.*;
import System.Xml.*;
import System.Xml.Schema.*;
public class Sample
{
private boolean mSuccess = true;
public Sample()
{
// Validate the document using an external XSD schema.
// Validation should fail.
Validate("notValidXSD.xml");
//Validate the document using an inline XSD. Validation should succeed.
Validate("inlineXSD.xml");
} //Sample
public static void main(String[] args)
{
Sample validation = new Sample();
} //main
private void Validate(String fileName)
{
mSuccess = true;
Console.WriteLine("\r\n******");
Console.WriteLine("Validating XML file " + fileName.ToString());
XmlTextReader txtReader = new XmlTextReader(fileName);
XmlValidatingReader reader = new XmlValidatingReader(txtReader);
// Set the validation event handler
reader.add_ValidationEventHandler(
new ValidationEventHandler(ValidationCallBack));
// Read XML data
while (reader.Read()) {
}
Console.WriteLine("Validation finished. Validation {0}",
(mSuccess == true) ? "successful!" : "failed.");
//Close the reader.
reader.Close();
} //Validate
//Display the validation error.
private void ValidationCallBack(Object sender, ValidationEventArgs args)
{
mSuccess = false;
Console.WriteLine("\r\n\tValidation error: " + args.get_Message());
} //ValidationCallBack
} //Sample
サンプルでは、次の入力ファイルを使用します。
notValidXSD.xml
<?xml version='1.0'?>
<bookstore xmlns="urn:bookstore-schema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:bookstore-schema books.xsd">
<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>
</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>
inlineXSD.xml
<store-data>
<!--Inline XSD schema-->
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<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="price" type="xsd:decimal"/>
</xsd:sequence>
<xsd:attribute name="genre" type="xsd:string"/>
</xsd:complexType>
</xsd:schema>
<!-- end of schema -->
<bookstore>
<book genre="novel">
<title>Pride And Prejudice</title>
<price>19.95</price>
</book>
</bookstore>
</store-data>
プラットフォーム
Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
バージョン情報
.NET Framework
サポート対象 : 2.0、1.1、1.0
参照
関連項目
XmlValidatingReader クラス
XmlValidatingReader メンバ
System.Xml 名前空間
XmlTextReader クラス