XmlReader Class

System.Xml Namespace

Provides forward-only, read-only access to XML data.

Introduction | Remarks | Syntax | Constructors | Properties | Methods | Inheritance | Exceptions

Introduction

The XmlReader class provides a high-performance, forward-only, read-only mechanism for reading XML data. It is designed for scenarios where you need to process large XML documents efficiently without loading the entire document into memory. This class supports various XML conformance levels and provides fine-grained control over how XML is read.

Remarks

XmlReader is an abstract class. To use it, you typically create an instance of one of its derived classes, such as XmlTextReader (for reading from text-based sources) or XmlDictionaryReader (for optimized reading of binary XML). The XmlReader.Create method is the recommended way to instantiate an XmlReader, as it can automatically detect the appropriate reader implementation based on the input stream or URI.

Key features of XmlReader include:

Syntax

public abstract class XmlReader : IDisposable

Inheritance

System.Object
    System.Xml.XmlReader
        System.Xml.XmlDictionaryReader
        System.Xml.XmlTextReader

Constructors

XmlReader is an abstract class and cannot be instantiated directly. Use the static XmlReader.Create method to create an instance.

Properties

Name Description
AttributeCount Gets the number of attributes on the current node.
BaseURI Gets the base URI of the current node.
CanReadBinaryContent Gets a value indicating whether the reader can read binary data.
CanResolveEntity Gets a value indicating whether the reader can resolve external entities.
Depth Gets the depth of the current node in the XML document.
EOF Gets a value indicating whether the reader is at the end of the stream.
HasAttributes Gets a value indicating whether the current node has attributes.
HasValue Gets a value indicating whether the current node has a value.
IsDefault Gets a value indicating whether the value of the current node is from an attribute that was defaulted.
IsEmptyElement Gets a value indicating whether the current node is an empty element (e.g., <MyElement/>).
Item(Int32) Gets the value of the attribute with the specified index.
Item(String) Gets the value of the attribute with the specified local name.
Item(String, String) Gets the value of the attribute with the specified local name and namespace.
LocalName Gets the local name of the current node.
Name Gets the qualified name of the current node.
NamespaceURI Gets the namespace URI of the current node.
NameTable Gets the XmlNameTable to use for tokenizing names.
NodeType Gets the type of the current node.
Prefix Gets the namespace prefix of the current node.
QuoteChar Gets the character used to quote attribute values.
ReadState Gets the state of the reader.
Value Gets the text value of the current node.
XmlLang Gets the current xml:lang attribute value.
XmlSpace Gets the current xml:space attribute value.

Methods

void Close()

Closes the stream to the underlying sources and releases the resources associated with the reader.

string GetAttribute(Int32 i)

Gets the value of the attribute with the specified index.

string GetAttribute(String name)

Gets the value of the attribute with the specified local name.

string GetAttribute(String localName, String namespaceURI)

Gets the value of the attribute with the specified local name and namespace.

string GetValue()

Gets the text value of the current node.

string LookupNamespace(String prefix)

Resolves the namespace prefix on the current node, or for the reader itself if it is not on an element node.

bool MoveToAttribute(Int32 index)

Moves to the attribute with the specified index.

bool MoveToAttribute(String name)

Moves to the attribute with the specified local name.

bool MoveToAttribute(String localName, String namespaceURI)

Moves to the attribute with the specified local name and namespace.

bool MoveToElement()

Moves to the element that contains the current attribute node.

bool MoveToFirstAttribute()

Moves to the first attribute of the current node.

bool MoveToNextAttribute()

Moves to the next attribute of the current node.

bool Read()

Reads the next node from the XML stream.

bool ReadAttributeValue()

Reads the attribute value of the current node.

void ReadEndElement()

Reads until the current node is an end element or a stream level is reached.

string ReadElementString()

Reads the text content of the current element and moves the reader to the next sibling.

string ReadElementString(String localName)

Reads the text content of an element with the specified local name and moves the reader to the next sibling.

string ReadElementString(String localName, String namespaceURI)

Reads the text content of an element with the specified local name and namespace and moves the reader to the next sibling.

string ReadInnerXml()

Reads the content of the current node (including markup) and returns it as a string.

string ReadOuterXml()

Reads the outer XML content of the current node and returns it as a string.

string ReadString()

Reads the text content of the current node and moves to the next node.

void ReadToFollowing(String name)

Reads until the next node with the specified local name is encountered.

bool ReadToFollowing(String name, String namespaceURI)

Reads until the next node with the specified local name and namespace is encountered.

bool ReadToNextSibling(String name)

Reads until the next sibling node with the specified local name is encountered.

bool ReadToNextSibling(String name, String namespaceURI)

Reads until the next sibling node with the specified local name and namespace is encountered.

bool ReadTo:'#text()'

Reads until the next text node is encountered.

void ReadStartElement()

Reads until the current node is an element or a stream level is reached.

void ReadStartElement(String localName)

Reads the start element with the specified local name.

void ReadStartElement(String localName, String namespaceURI)

Reads the start element with the specified local name and namespace.

void ResolveEntity()

Resolves the current DTD entity.

Exceptions

Exception Type Condition
ArgumentException The XmlReaderSettings object is invalid.
XmlException An error occurred while parsing the XML.
IOException An I/O error occurred.
OutOfMemoryException Not enough memory is available to process the request.