XmlTextReader.ReadChars(Char[], Int32, Int32) メソッド

定義

要素のテキストの内容を文字バッファーに読み取ります。 このメソッドは、連続して呼び出すことによって埋め込みテキストの大量のストリームを読み取るように設計されています。

public:
 int ReadChars(cli::array <char> ^ buffer, int index, int count);
public int ReadChars (char[] buffer, int index, int count);
member this.ReadChars : char[] * int * int -> int
Public Function ReadChars (buffer As Char(), index As Integer, count As Integer) As Integer

パラメーター

buffer
Char[]

テキストの内容が書き込まれるバッファーとして機能する文字の配列。

index
Int32

メソッドがテキストの内容の書き込みを開始できる buffer 内の位置。

count
Int32

buffer に書き込む文字数。

戻り値

読み取った文字数。 リーダーが要素に配置されていない場合、または返す対象となるテキストの内容が現在のコンテキスト内にこれ以上ない場合は、0 になる可能性があります。

例外

countbuffer (バッファー サイズ - index) で指定した値を超えています。

buffer 値は null です。

index< 0 または count< 0

次の例では、 を使用して ReadCharsXML を読み取ります。

#using <System.Xml.dll>

using namespace System;
using namespace System::Xml;

// Reads an XML document using ReadChars
int main()
{
   XmlTextReader^ reader = nullptr;
   String^ filename = "items.xml";
   try
   {
      
      // Declare variables used by ReadChars
      array<Char>^buffer;
      int iCnt = 0;
      int charbuffersize;
      
      // Load the reader with the data file.  Ignore white space.
      reader = gcnew XmlTextReader( filename );
      reader->WhitespaceHandling = WhitespaceHandling::None;
      
      // Set variables used by ReadChars.
      charbuffersize = 10;
      buffer = gcnew array<Char>(charbuffersize);
      
      // Parse the file.  Read the element content
      // using the ReadChars method.
      reader->MoveToContent();
      while ( (iCnt = reader->ReadChars( buffer, 0, charbuffersize )) > 0 )
      {
         
         // Print out chars read and the buffer contents.
         Console::WriteLine( "  Chars read to buffer:{0}", iCnt );
         Console::WriteLine( "  Buffer: [{0}]", gcnew String( buffer,0,iCnt ) );
         
         // Clear the buffer.
         Array::Clear( buffer, 0, charbuffersize );
      }
   }
   finally
   {
      if ( reader != nullptr )
            reader->Close();
   }

}
using System;
using System.Xml;

// Reads an XML document using ReadChars

public class Sample {

  private const String filename = "items.xml";

  public static void Main() {

    XmlTextReader reader = null;

    try {

      // Declare variables used by ReadChars
      Char []buffer;
      int iCnt = 0;
      int charbuffersize;

      // Load the reader with the data file.  Ignore white space.
      reader = new XmlTextReader(filename);
      reader.WhitespaceHandling = WhitespaceHandling.None;

      // Set variables used by ReadChars.
      charbuffersize = 10;
      buffer = new Char[charbuffersize];

      // Parse the file.  Read the element content
      // using the ReadChars method.
      reader.MoveToContent();
      while ( (iCnt = reader.ReadChars(buffer,0,charbuffersize)) > 0 ) {
        // Print out chars read and the buffer contents.
        Console.WriteLine ("  Chars read to buffer:" + iCnt);
        Console.WriteLine ("  Buffer: [{0}]", new String(buffer,0,iCnt));
        // Clear the buffer.
        Array.Clear(buffer,0,charbuffersize);
      }
    }
    finally {
      if (reader!=null)
        reader.Close();
    }
  }
} // End class
Imports System.Xml

' Reads an XML document using ReadChars
Public Class Sample
    Private Const filename As String = "items.xml"
    
    Public Shared Sub Main()
        Dim reader As XmlTextReader = Nothing
        
        Try
            ' Declare variables used by ReadChars
            Dim buffer() As Char
            Dim iCnt As Integer = 0
            Dim charbuffersize As Integer
            
            ' Load the reader with the data file.  Ignore white space.
            reader = New XmlTextReader(filename)
            reader.WhitespaceHandling = WhitespaceHandling.None
            
            ' Set variables used by ReadChars.
            charbuffersize = 10
            buffer = New Char(charbuffersize) {}
            
            ' Parse the file.  Read the element content  
            ' using the ReadChars method.
            reader.MoveToContent()
            iCnt = reader.ReadChars(buffer,0,charbuffersize)
            while (iCnt > 0)
              ' Print out chars read and the buffer contents.
              Console.WriteLine("  Chars read to buffer:" & iCnt)
              Console.WriteLine("  Buffer: [{0}]", New String(buffer, 0, iCnt))
              ' Clear the buffer.
              Array.Clear(buffer, 0, charbuffersize)
              iCnt = reader.ReadChars(buffer,0,charbuffersize)
           end while

        Finally
            If Not (reader Is Nothing) Then
                reader.Close()
            End If
        End Try
    End Sub 
End Class

この例は、items.xml ファイルを入力として使用します。


<?xml version="1.0"?>
<!-- This is a sample XML document -->
<!DOCTYPE Items [<!ENTITY number "123">]>
<Items>
  <Item>Test with an entity: &number;</Item>
  <Item>test with a child element <more/> stuff</Item>
  <Item>test with a CDATA section <![CDATA[<456>]]> def</Item>
  <Item>Test with an char entity: A</Item>
  <!-- Fourteen chars in this element.-->
  <Item>1234567890ABCD</Item>
</Items>

注釈

注意

.NET Framework 2.0 以降では、 メソッドを使用してXmlReader.Create新しい機能を利用してインスタンスを作成XmlReaderすることをお勧めします。

これは、XML ドキュメントに埋め込まれた非常に大きなテキスト ストリームを処理する最も効率的な方法です。 大きな文字列オブジェクトを割り当てるのではなく、 ReadChars テキスト コンテンツを一度にバッファーとして返します。 このメソッドは、要素ノードでのみ機能するように設計されています。 その他のノードの種類では ReadChars 、 が返されます 0

次の XML では、リーダーが開始タグに配置されている場合は、 を返testし、ReadCharsリーダーを終了タグの後に配置します。

<Item>test</Item>

ReadChars には、次の機能があります。

  • このメソッドは、要素ノードでのみ機能するように設計されています。 その他のノードの種類では ReadChars 、0 が返されます。

  • このメソッドは、実際の文字コンテンツを返します。 エンティティ、CDATA、またはその他のマークアップを解決しようとすることはありません。 ReadChars は、マークアップを含む開始タグと終了タグの間のすべてを返します。

  • ReadChars は、整形式ではない XML マークアップを無視します。 たとえば、次の XML 文字列<A>1<A>2</A>ReadCharsを読み取ると、 は を返します1<A>2</A>。 (一致する要素ペアからマークアップを返し、他の要素を無視します)。

  • このメソッドは正規化を行いません。

  • 文字ストリームの末尾に達すると ReadChars 、値 0 が返され、リーダーは終了タグの後に配置されます。

  • 属性の読み取りメソッドは、 の使用中 ReadCharsは使用できません。

たとえば、次の XML を使用します。

<thing>
 some text
</thing>
<item>
</item>

リーダーは while ループの <item> 末尾にある 要素に配置されます。

if (XmlNodeType.Element == reader.NodeType && "thing" == reader.Name)
{
 while(0 != reader.ReadChars(buffer, 0, 1)
 {
 // Do something.
 // Attribute values are not available at this point.
 }
}

適用対象

こちらもご覧ください