System.Xml.XmlTextReader.ReadChars Method

Reads the text contents of an element into a character buffer. This method is designed to read large streams of embedded text by calling it successively.

Syntax

public int ReadChars (char[] buffer, int index, int count)

Parameters

buffer
The array of characters that serves as the buffer to which the text contents are written.
index
The position within buffer where the method can begin writing text contents.
count
The number of characters to write into buffer.

Returns

The number of characters read. This can be 0 if the reader is not positioned on an element or if there is no more text content to return in the current context.

Exceptions

TypeReason
ArgumentException count > buffer.Length - index.
ArgumentNullException buffer is null.
ArgumentOutOfRangeException index < 0, or count < 0.

Remarks

Note:

In the dnprdnext release, the recommended practice is to create System.Xml.XmlReader instances using the erload:System.Xml.XmlReader.Create method. This allows you to take full advantage of the new features introduced in this release. For more information, see Creating XML Readers.

This is the most efficient way to process very large streams of text embedded in an XML document. Rather than allocating large string objects, ReadChars returns text content a buffer at a time. This method is designed to work only on element nodes. Other node types cause ReadChars to return 0.

In the following XML, if the reader is positioned on the start tag, ReadChars returns test and positions the reader after the end tag.

Example

<Item>test</Item>

ReadChars has the following functionality:

  • This method is designed to work on element nodes only. Other node types cause ReadChars to return 0.

  • This method returns the actual character content. There is no attempt to resolve entities, CDATA, or any other markup encountered. ReadChars returns everything between the start tag and the end tag, including markup.

  • ReadChars ignores XML markup that is not well-formed. For example, when reading the following XML string <A>1<A>2</A>, ReadChars returns 1<A>2</A>. (It returns markup from the matching element pair and ignores others.)

  • This method does not do any normalization.

  • When ReadChars has reached the end of the character stream, it returns the value 0 and the reader is positioned after the end tag.

  • Attribute read methods are not available while using ReadChars.

For example, using the following XML:

Example

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

The reader is positioned on the <item> element at the end of the while loop.

Example

 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.
  }
 }

Requirements

Namespace: System.Xml
Assembly: System.Xml (in System.Xml.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0