XmlTextReader.ReadChars メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > XmlTextReader.ReadChars メソッドの意味・解説 

XmlTextReader.ReadChars メソッド

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

名前空間: System.Xml
アセンブリ: System.Xml (system.xml.dll 内)
構文構文

例外例外
例外種類条件

ArgumentException

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

ArgumentNullException

buffer 値が null 参照 (Visual Basic では Nothing) です。

ArgumentOutOfRangeException

index < 0 または count< 0

解説解説

XML ドキュメント埋め込まれテキスト大量ストリーム処理するには、これが最も効率的な方法です。ReadChars は、大きな文字列オブジェクト割り当てるではなく一度1 つバッファテキストの内容返します。このメソッドは、要素ノードだけで機能するように設計されています。他のノード型では、ReadChars0返します

リーダー開始タグ配置すると、ReadCharstest返しリーダー終了タグ後ろ配置する XML次に示します

<Item>test</Item>

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.
  }
 }
使用例使用例

ReadChars使用して XML読み取る例を次に示します

Imports System
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 
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
#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();
   }

}

import System.*;
import System.Xml.*;

// Reads an XML document using ReadChars
public class Sample
{
    private static String fileName = "items.xml";

    public static void main(String[]
 args)
    {
        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.set_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();
            }
        }
    } //main
} // End class Sample

この例では、入力として 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: &#65;</Item>
  <!-- Fourteen chars in this element.-->
  <Item>1234567890ABCD</Item>
</Items>
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照


このページでは「.NET Framework クラス ライブラリ リファレンス」からXmlTextReader.ReadChars メソッドを検索した結果を表示しています。
Weblioに収録されているすべての辞書からXmlTextReader.ReadChars メソッドを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からXmlTextReader.ReadChars メソッド を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

XmlTextReader.ReadChars メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



XmlTextReader.ReadChars メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS