XmlReadState Enumeration
Summary
Specifies the state of the XmlReader
.
This enumeration is used by the XmlReader.ReadState
property to indicate the current position and status of the reader within the XML document.
Members
Initial
The XmlReader is at its initial state, before the first call to
Read()
.
BeforeOpen
The XmlReader is positioned before the first node and before the
Read()
method has been called.
Reading
The XmlReader is currently reading the node.
Interactive
The XmlReader is in an interactive state, for example, after
ReadAttributeValue()
has been called.
Error
An error occurred while reading the XML. The
Exception
property contains details about the error.
EndOfFile
The end of the XML document has been reached.
Closed
The XmlReader has been closed.
Usage Example
The following example demonstrates how to check the ReadState
of an XmlReader
.
using System;
using System.Xml;
public class Example
{
public static void Main(string[] args)
{
string xmlString = @"<root><item>data</item></root>";
// Use StringReader to read from a string
using (XmlReader reader = XmlReader.Create(new System.IO.StringReader(xmlString)))
{
Console.WriteLine("Initial ReadState: {0}", reader.ReadState); // Output: Initial
while (reader.Read())
{
// Reading state
Console.WriteLine("Current ReadState: {0}", reader.ReadState);
if (reader.NodeType == XmlNodeType.Element)
{
Console.WriteLine(" Element: {0}", reader.Name);
}
else if (reader.NodeType == XmlNodeType.Text)
{
Console.WriteLine(" Text: {0}", reader.Value);
}
}
Console.WriteLine("Final ReadState: {0}", reader.ReadState); // Output: EndOfFile
}
}
}
Requirements
Namespace | System.Xml |
---|---|
Assembly | System.Xml.dll |