Reads the string value of the current node.
This method is intended for cases where the current node is a value node (such as an element's text content or an attribute's value) and you want to retrieve its string representation.
A string
representing the value of the current node. Returns an empty string if the node has no value.
Value
.NodeType
property before calling ReadString
.ReadContentAsString()
for more control.
using System;
using System.Xml;
public class Example
{
public static void Main(string[] args)
{
string xml = "<root><message>Hello, World!</message></root>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
XmlNodeReader reader = new XmlNodeReader(doc.DocumentElement);
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element && reader.Name == "message")
{
if (reader.Read()) // Move to the text content
{
string message = reader.ReadString();
Console.WriteLine($"Message: {message}"); // Output: Message: Hello, World!
}
}
}
}
}