XmlValidatingReader コンストラクター

定義

XmlValidatingReader クラスの新しいインスタンスを初期化します。

オーバーロード

XmlValidatingReader(XmlReader)

指定した XmlReader から返される内容を検証する XmlValidatingReader クラスの新しいインスタンスを初期化します。

XmlValidatingReader(Stream, XmlNodeType, XmlParserContext)

値を指定して、XmlValidatingReader クラスの新しいインスタンスを初期化します。

XmlValidatingReader(String, XmlNodeType, XmlParserContext)

値を指定して、XmlValidatingReader クラスの新しいインスタンスを初期化します。

XmlValidatingReader(XmlReader)

Source:
XmlValidatingReader.cs
Source:
XmlValidatingReader.cs
Source:
XmlValidatingReader.cs

指定した XmlReader から返される内容を検証する XmlValidatingReader クラスの新しいインスタンスを初期化します。

public:
 XmlValidatingReader(System::Xml::XmlReader ^ reader);
public XmlValidatingReader (System.Xml.XmlReader reader);
new System.Xml.XmlValidatingReader : System.Xml.XmlReader -> System.Xml.XmlValidatingReader
Public Sub New (reader As XmlReader)

パラメーター

reader
XmlReader

検証中に読み取る対象の XmlReader。 現在の実装では、XmlTextReader のみサポートします。

例外

指定したリーダーが XmlTextReader ではありません。

次の例では、2 つのドキュメントを検証します。

#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;
}

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 );
  }
}
Imports System.IO
Imports System.Xml
Imports System.Xml.Schema

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

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

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>

注釈

Note

クラスはXmlValidatingReader、.NET Framework 2.0 では使用されていません。 クラスと メソッドを使用XmlReaderSettingsして、検証XmlReaderインスタンスをCreate作成できます。 詳細については、XmlReader のリファレンス ページの「解説」を参照してください。

指定 XmlReader された から返されたすべてのノードも、この検証リーダーから返されるため、プロセスに情報が失われる可能性はありません。 基になるリーダーから返されない新しいノードは、このリーダー (既定の属性やエンティティ参照の子など) によって追加できます。 指定 XmlTextReader された に設定されたプロパティは、この検証リーダーにも適用されます。 たとえば、指定されたリーダーに WhitespaceHandling.None が設定されている場合、この検証リーダーは空白も無視します。

検証に外部ドキュメント型定義 (DTD) またはスキーマが必要な場合、 プロパティは XmlResolver 、外部リソースの XmlResolver 解決に使用するオブジェクトを設定します。

こちらもご覧ください

適用対象

XmlValidatingReader(Stream, XmlNodeType, XmlParserContext)

Source:
XmlValidatingReader.cs
Source:
XmlValidatingReader.cs
Source:
XmlValidatingReader.cs

値を指定して、XmlValidatingReader クラスの新しいインスタンスを初期化します。

public:
 XmlValidatingReader(System::IO::Stream ^ xmlFragment, System::Xml::XmlNodeType fragType, System::Xml::XmlParserContext ^ context);
public XmlValidatingReader (System.IO.Stream xmlFragment, System.Xml.XmlNodeType fragType, System.Xml.XmlParserContext context);
new System.Xml.XmlValidatingReader : System.IO.Stream * System.Xml.XmlNodeType * System.Xml.XmlParserContext -> System.Xml.XmlValidatingReader
Public Sub New (xmlFragment As Stream, fragType As XmlNodeType, context As XmlParserContext)

パラメーター

xmlFragment
Stream

解析する XML フラグメントを含んでいるストリーム。

fragType
XmlNodeType

XML フラグメントの XmlNodeType。 これは、フラグメントに何を含めることができるかを判断します (次の表を参照)。

context
XmlParserContext

XML フラグメントの解析先の XmlParserContext。 これには、使用する XmlNameTable、エンコーディング、名前空間スコープ、現在の xml:lang、および xml:space スコープが含まれます。

例外

fragType が、次の表に示すノード型の 1 つではありません。

注釈

Note

クラスはXmlValidatingReader、.NET Framework 2.0 では使用されていません。 クラスと メソッドを使用XmlReaderSettingsして、検証XmlReaderインスタンスをCreate作成できます。 詳細については、XmlReader のリファレンス ページの「解説」を参照してください。

このコンストラクターは、指定された文字列を XML のフラグメントとして解析します。 XML フラグメントが要素または属性の場合は、整形式の XML ドキュメントのルート レベル規則をバイパスできます。

次の表は、 の有効な値 fragType と、リーダーが各ノードの種類を解析する方法を示しています。

XmlNodeType フラグメントに含まれる可能性がある
要素 有効な要素コンテンツ (たとえば、要素、コメント、処理命令、cdata、テキスト、エンティティ参照の任意の組み合わせ)。
属性 属性の値 (引用符内の部分)。
ドキュメント XML ドキュメント全体の内容。これにより、ドキュメント レベルのルールが適用されます。

リーダーは、次の手順を使用してストリームのエンコードを決定します。

  1. プロパティを XmlParserContext.Encoding 調べてエンコードを決定します。

  2. プロパティが Encoding の場合、リーダーは nullストリームの先頭でバイト順マークをチェックします。

  3. プロパティが Encodingnull、バイト順マークが見つからない場合、リーダーはストリームが UTF-8 でエンコードされていることを前提としています。

このリーダーがドキュメント型定義 (DTD) (つまり、 ValidationType ValidationType.DTD または ValidationType.Auto XmlParserContext に設定されている) を使用して検証する場合、コンストラクターで指定された は、必要なすべての DocumentType 情報を指定する必要があります。

Note

DTD を使用してフラグメントを検証することはできません。 定義上、DTD では、検証のためにドキュメント全体を読み込む必要があります。

このリーダーが XML-Data Reduced (XDR) または XML スキーマ定義言語 (XSD) スキーマを使用して検証する場合は、 プロパティを Schemas 使用してスキーマを含む を指定 XmlSchemaCollection します (つまり、 XmlParserContext は DocumentType 情報を指定する必要はありません)。

こちらもご覧ください

適用対象

XmlValidatingReader(String, XmlNodeType, XmlParserContext)

Source:
XmlValidatingReader.cs
Source:
XmlValidatingReader.cs
Source:
XmlValidatingReader.cs

値を指定して、XmlValidatingReader クラスの新しいインスタンスを初期化します。

public:
 XmlValidatingReader(System::String ^ xmlFragment, System::Xml::XmlNodeType fragType, System::Xml::XmlParserContext ^ context);
public XmlValidatingReader (string xmlFragment, System.Xml.XmlNodeType fragType, System.Xml.XmlParserContext context);
new System.Xml.XmlValidatingReader : string * System.Xml.XmlNodeType * System.Xml.XmlParserContext -> System.Xml.XmlValidatingReader
Public Sub New (xmlFragment As String, fragType As XmlNodeType, context As XmlParserContext)

パラメーター

xmlFragment
String

解析する XML フラグメントを含んでいる文字列。

fragType
XmlNodeType

XML フラグメントの XmlNodeType。 これは、フラグメント文字列に何を含めることができるかも判断します (次の表を参照)。

context
XmlParserContext

XML フラグメントの解析先の XmlParserContext。 これには、使用する NameTable、エンコーディング、名前空間スコープ、現在の xml:lang、および xml:space スコープが含まれます。

例外

fragType が、次の表に示すノード型の 1 つではありません。

次の例では、XML フラグメントを読み取ります。 と をXmlNamespaceManager使用XmlParserContextして名前空間の一致を処理します。

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

public class Sample
{
    public static void Main()
    {
        XmlTextReader reader = null;

        try
        {
            //Create the XML fragment to be parsed.
            string xmlFrag = "<book> " +
                            "<title>Pride And Prejudice</title>" +
                            "<bk:genre>novel</bk:genre>" +
                            "</book>";

            //Create the XmlNamespaceManager that is used to
            //look up namespace information.
            NameTable nt = new NameTable();
            XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
            nsmgr.AddNamespace("bk", "urn:sample");

            //Create the XmlParserContext.
            XmlParserContext context = new XmlParserContext(null, nsmgr, null, XmlSpace.None);

            //Implement the reader.
            reader = new XmlTextReader(xmlFrag, XmlNodeType.Element, context);

            //Parse the XML fragment.  If they exist, display the
            //prefix and namespace URI of each element.
            while (reader.Read())
            {
                if (reader.IsStartElement())
                {
                    if (string.IsNullOrEmpty(reader.Prefix))
                    {
                        Console.WriteLine("<{0}>", reader.LocalName);
                    }
                    else
                    {
                        Console.Write("<{0}:{1}>", reader.Prefix, reader.LocalName);
                        Console.WriteLine(" The namespace URI is " + reader.NamespaceURI);
                    }
                }
            }
        }

        finally
        {
            if (reader != null)
                reader.Close();
        }
    }
} // End class
Imports System.IO
Imports System.Xml

Public Class Sample

    Public Shared Sub Main()
        Dim reader As XmlTextReader = Nothing

        Try
            'Create the XML fragment to be parsed.
            Dim xmlFrag As String = "<book> " & _
                                    "<title>Pride And Prejudice</title>" & _
                                    "<bk:genre>novel</bk:genre>" & _
                                    "</book>"

            'Create the XmlNamespaceManager that is used to
            'look up namespace information.
            Dim nt As New NameTable()
            Dim nsmgr As New XmlNamespaceManager(nt)
            nsmgr.AddNamespace("bk", "urn:sample")

            'Create the XmlParserContext.
            Dim context As New XmlParserContext(Nothing, nsmgr, Nothing, XmlSpace.None)

            'Implement the reader. 
            reader = New XmlTextReader(xmlFrag, XmlNodeType.Element, context)

            'Parse the XML fragment.  If they exist, display the   
            'prefix and namespace URI of each element.
            While reader.Read()
                If reader.IsStartElement() Then
                    If reader.Prefix = String.Empty Then
                        Console.WriteLine("<{0}>", reader.LocalName)
                    Else
                        Console.Write("<{0}:{1}>", reader.Prefix, reader.LocalName)
                        Console.WriteLine(" The namespace URI is " & reader.NamespaceURI)
                    End If
                End If
            End While
        Finally
            If Not (reader Is Nothing) Then
                reader.Close()
            End If
        End Try
    End Sub
End Class

注釈

Note

クラスはXmlValidatingReader、.NET Framework 2.0 では使用されていません。 クラスと メソッドを使用XmlReaderSettingsして、検証XmlReaderインスタンスをCreate作成できます。 詳細については、XmlReader のリファレンス ページの「解説」を参照してください。

このコンストラクターは、指定された文字列を XML のフラグメントとして解析します。 XML フラグメントが要素または属性の場合は、整形式の XML ドキュメントのルート レベル規則をバイパスできます。 このコンストラクターは、 から ReadInnerXml返された文字列を処理できます。

次の表は、 の有効な値 fragType と、リーダーが各ノードの種類を解析する方法を示しています。

XmlNodeType フラグメントに含まれる可能性がある
要素 有効な要素コンテンツ (たとえば、要素、コメント、処理命令、cdata、テキスト、エンティティ参照の任意の組み合わせ)。
属性 属性の値 (引用符内の部分)。
ドキュメント XML ドキュメント全体の内容。これにより、ドキュメント レベルのルールが適用されます。

このリーダーがドキュメント型定義 (DTD) (つまり、 ValidationType ValidationType.DTD または ValidationType.Auto XmlParserContext に設定されている) を使用して検証する場合、コンストラクターで指定された は、必要なすべての DocumentType 情報を指定する必要があります。

Note

DTD を使用してフラグメントを検証することはできません。 定義上、DTD では、検証のためにドキュメント全体を読み込む必要があります。

このリーダーが XML-Data Reduced (XDR) または XML スキーマ定義言語 (XSD) スキーマを使用して検証する場合は、 プロパティを Schemas 使用してスキーマを含む を指定 XmlSchemaCollection します ( XmlParserContext DocumentType 情報を指定する必要はありません)。

こちらもご覧ください

適用対象