XmlReader Class

public abstract class XmlReader

Implements a non-cached, forward-only reader as described by the XML Information Set.

This class provides read-only access to XML data. It is optimized for performance and memory efficiency, making it ideal for processing large XML documents. XmlReader allows you to move through an XML document node by node, accessing information such as the node type, name, value, and attributes.

Inheritance

  • System.Object
  • System.Xml.XmlReader

Derived classes

The XmlReader class is inherited by the following classes:

Remarks

XmlReader offers a more efficient way to read XML than using the XmlDocument object model. It does not load the entire document into memory, which reduces memory consumption for large files. Instead, it processes the XML stream sequentially.

Key features of XmlReader include:

  • Forward-only access: You can only move forward through the XML document.
  • Non-cached: Data is read as needed, minimizing memory usage.
  • Node-based processing: Allows granular access to each XML node (elements, attributes, text, etc.).
  • Support for various XML formats: Can read well-formed XML documents.

Examples

The following example demonstrates how to read an XML file using XmlReader and print the node type and value:


using System;
using System.Xml;

public class Example
{
    public static void Main(string[] args)
    {
        string xmlString = @"
            <?xml version=""1.0"" encoding=""utf-8""?>
            <catalog>
                <book id=""bk101"">
                    <author>Gambardella, Matthew</author>
                    <title>XML Developer's Guide</title>
                    <genre>Computer</genre>
                    <price>44.95</price>
                    <publish_date>2000-10-01</publish_date>
                    <description>An in-depth look at creating applications with XML.</description>
                </book>
                <book id=""bk102"">
                    <author>Ralls, Kim</author>
                    <title>Midnight Rain</title>
                    <genre>Fantasy</genre>
                    <price>5.95</price>
                    <publish_date>2000-12-16</publish_date>
                    <description>A former architect battles corporate zombies.</description>
                </book>
            </catalog>";

        using (XmlReader reader = XmlReader.Create(new System.IO.StringReader(xmlString)))
        {
            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Element:
                        Console.Write($"<{reader.Name}>");
                        if (reader.HasAttributes)
                        {
                            while (reader.MoveToNextAttribute())
                            {
                                Console.Write($" {reader.Name}='{reader.Value}'");
                            }
                            reader.MoveToElement(); // Move back to the element node
                        }
                        Console.WriteLine();
                        break;
                    case XmlNodeType.Text:
                        Console.WriteLine($"  {reader.Value}");
                        break;
                    case XmlNodeType.XmlDeclaration:
                    case XmlNodeType.ProcessingInstruction:
                        Console.WriteLine($"");
                        break;
                    case XmlNodeType.EntityReference:
                        Console.WriteLine($"&{reader.Name};");
                        break;
                    case XmlNodeType.EndElement:
                        Console.WriteLine($"</{reader.Name}>");
                        break;
                }
            }
        }
    }
}
                

Methods

Key methods include:

  • Read(): Advances the reader to the next node.
  • MoveToAttribute(string name): Moves the reader to the attribute with the specified name.
  • ReadAttributeValue(): Reads the value of the current attribute.
  • ReadElementString(): Reads the text content of a simple element.
  • ReadInnerXml(): Reads all the markup representing the content of the current node.
  • ReadOuterXml(): Reads all the markup representing the current node and all its content.
  • Close(): Closes the stream and underlying streams.

Properties

Key properties include:

  • NodeType: Gets the type of the current node.
  • Name: Gets the qualified name of the current node.
  • Value: Gets the value of the current node.
  • HasAttributes: Gets a value indicating whether the current node has attributes.
  • AttributeCount: Gets the number of attributes on the current node.
  • IsEmptyElement: Gets a value indicating whether the current node is an empty element tag (e.g. <MyElement/>).
  • EOF: Gets a value indicating whether the reader is at the end of the stream.

See Also