XmlTextReader.ReadChars メソッド
アセンブリ: System.Xml (system.xml.dll 内)

Public Function ReadChars ( _ buffer As Char(), _ index As Integer, _ count As Integer _ ) As Integer
Dim instance As XmlTextReader Dim buffer As Char() Dim index As Integer Dim count As Integer Dim returnValue As Integer returnValue = instance.ReadChars(buffer, index, count)
戻り値
読み取った文字数。リーダーが要素に配置されていない場合、または返す対象となるテキストの内容が現在のコンテキスト内にこれ以上ない場合は、0 になる可能性があります。


![]() |
---|
Microsoft .NET Framework version 2.0 リリースでは、System.Xml.XmlReader.Create メソッドを使用して XmlReader インスタンスを作成することをお勧めします。これにより、このリリースで導入された新機能を十分に活用できます。詳細については、「XML リーダーの作成」を参照してください。 |
XML ドキュメントに埋め込まれたテキストの大量のストリームを処理するには、これが最も効率的な方法です。ReadChars は、大きな文字列オブジェクトを割り当てるのではなく、一度に 1 つのバッファにテキストの内容を返します。このメソッドは、要素ノードだけで機能するように設計されています。他のノード型では、ReadChars は 0 を返します。
リーダーを開始タグに配置すると、ReadChars が test を返し、リーダーを終了タグの後ろに配置する XML を次に示します。
-
このメソッドは、要素ノードだけで機能するように設計されています。他のノード型では、ReadChars は 0 を返します。
-
このメソッドは、実際の文字の内容を返します。検出したエンティティ、CDATA、または他のマークアップの解決は試行されません。ReadChars は、開始タグと終了タグの間の、マークアップを含むすべての内容を返します。
-
ReadChars は、整形式でない XML マークアップを無視します。たとえば、次の XML 文字列 <A>1<A>2</A> を読み取ると、ReadChars は 1<A>2</A> を返します。一致する要素ペアからマークアップを返し、それ以外を無視します。

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: A</Item> <!-- Fourteen chars in this element.--> <Item>1234567890ABCD</Item> </Items>

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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からXmlTextReader.ReadChars メソッドを検索する場合は、下記のリンクをクリックしてください。

- XmlTextReader.ReadChars メソッドのページへのリンク