System.Xml.XmlNodeReader.ReadChar()
Reads the next character from the current node. This method is an optimization for reading text nodes.
Syntax
Parameters
This method has no parameters.
Return Value
char
The next character in the current node. Returns '\0'
if there are no more characters to read.
Remarks
This method provides a fast way to read character by character from an XmlNodeReader
. It is particularly useful when processing large text nodes where character-level access is required.
The XmlNodeReader
exposes the XML document as a sequence of nodes. The ReadChar()
method operates on the content of the current node. If the current node is not a text-related node (such as XmlNodeType.Text
, XmlNodeType.CDATA
, or XmlNodeType.SignificantWhitespace
), calling this method might throw an exception or return unexpected results.
Requirements
Namespace: System.Xml
Assembly: System.Xml.dll
Methods
- Read()
- ReadAttributeValue()
- ReadChar()
- ReadContentAsBase64()
- ReadContentAsChar()
- ReadContentAsDateTime()
- ReadContentAsDecimal()
- ReadContentAsDouble()
- ReadContentAsInt()
- ReadContentAsLong()
- ReadElementContentAsString()
Example
C# Example
using System;
using System.Xml;
public class Example
{
public static void Main(string[] args)
{
// Assume 'xmlDocument' is an initialized XmlDocument
// and 'rootElement' is a specific element within it.
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml("<root><message>Hello, World!</message></root>");
XmlNodeReader reader = new XmlNodeReader(xmlDoc.DocumentElement);
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Text)
{
Console.WriteLine("Processing text node:");
char c;
while ((c = reader.ReadChar()) != '\0')
{
Console.Write(c);
}
Console.WriteLine(); // New line after reading all characters
}
}
}
}
Note
This method is designed for efficient character-by-character reading. For reading entire text content, consider using Value
property or ReadContentAsString()
.