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

例外

count 大于 buffer 中指定的空间(缓冲区大小 - index)。

buffer 值为 null

index< 0count< 0

示例

以下示例使用 ReadChars在 XML 中读取 。

#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 中,如果读取器位于开始标记上, ReadCharstest 返回读取器并将其定位在结束标记之后。

<Item>test</Item>

ReadChars 具有以下功能:

  • 此方法仅用于元素节点。 其他节点类型导致 ReadChars 返回 0。

  • 此方法返回实际字符内容。 不会尝试解析遇到的实体、CDATA 或任何其他标记。 ReadChars 返回开始标记和结束标记之间的所有内容,包括标记。

  • ReadChars 忽略格式不正确的 XML 标记。 例如,读取以下 XML 字符串 <A>1<A>2</A>时, ReadChars 返回 1<A>2</A>。 (它从匹配的元素对返回标记,并忽略 others.)

  • 此方法不执行任何规范化。

  • 当已到达字符流的末尾时 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.
 }
}

适用于

另请参阅