.NET API Documentation

XmlReaderImplementation Class

System.Xml abstract class XmlReaderImplementation

Represents a reader that provides fast, forward-only access to a stream of XML data. This is the base class for all XmlReader implementations.

Remarks

The XmlReader class provides a way to read XML documents in a forward-only, read-only manner. This makes it very efficient for processing large XML files. The XmlReaderImplementation abstract class defines the core functionality and interface that all concrete XmlReader classes must adhere to.

Key features include:

Members

See Also

Example

The following example demonstrates basic usage of an XmlReader to iterate through XML nodes. Note that XmlReaderImplementation itself is abstract and cannot be instantiated directly. Concrete implementations like XmlTextReader or XmlDictionaryReader are used.

// This example uses XmlTextReader, a concrete implementation of XmlReader.
using System;
using System.Xml;

public class XmlReaderExample
{
    public static void Main(string[] args)
    {
        string xmlString = @"<bookstore><book category='cooking'><title lang='en'>Everyday Italian</title><author>Giada De Laurentiis</author><year>2005</year><price>30.00</price></book></bookstore>";

        using (XmlReader reader = XmlReader.Create(new System.IO.StringReader(xmlString)))
        {
            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Element:
                        Console.WriteLine($"<{reader.Name}>");
                        if (reader.HasAttributes)
                        {
                            while (reader.MoveToNextAttribute())
                            {
                                Console.WriteLine($"  Attribute: {reader.Name} = {reader.Value}");
                            }
                        }
                        break;
                    case XmlNodeType.Text:
                        Console.WriteLine($"  Text: {reader.Value}");
                        break;
                    case XmlNodeType.XmlDeclaration:
                    case XmlNodeType.DocumentType:
                        Console.WriteLine($"<?{reader.Name}... ?>");
                        break;
                    case XmlNodeType.EndElement:
                        Console.WriteLine($"</{reader.Name}>");
                        break;
                }
            }
        }
    }
}