Summary

Represents a reader that provides fast, forward-only access to XML data as a stream of characters.

This class implements the XmlReader interface and provides a way to read XML documents from various sources, such as files, streams, and strings. It is optimized for performance and is suitable for scenarios where random access to XML data is not required.

Syntax

public class XmlTextReader : XmlReader

Constructors

public XmlTextReader(Stream input);

Initializes a new instance of the XmlTextReader class with the specified stream.

public XmlTextReader(string url);

Initializes a new instance of the XmlTextReader class with the specified URL.

public XmlTextReader(TextReader reader);

Initializes a new instance of the XmlTextReader class with the specified TextReader.

Properties

Attribute Properties

  • Name: string - Gets the qualified name of the current node.
  • Value: string - Gets the text value of the current node.
  • AttributeCount: int - Gets the number of attributes on the current node.

Node Properties

  • NodeType: XmlNodeType - Gets the node type of the current node.
  • LocalName: string - Gets the local name of the current node.
  • NameTable: XmlNameTable - Gets an XmlNameTable used for comparing names.

Miscellaneous Properties

  • BaseURI: string - Gets the base URI of the current node.
  • EOF: bool - Gets a value indicating whether the reader is at the end of the stream.

Methods

Reading Methods

  • Read(): bool - Reads the next node from the XML stream.
  • ReadAttributeValue(): string - Reads the value of an attribute.
  • ReadElementString(): void - Reads the text content of a simple element.

Navigation Methods

  • MoveToAttribute(int i): bool - Moves to the attribute with the specified index.
  • MoveToElement(): bool - Moves the reader to the element that owns the current attribute.
  • ReadToFollowing(string name): bool - Moves the reader to the next element with the specified name.

Disposal Methods

  • Close(): void - Closes the stream and the underlying stream.
  • Dispose(): void - Releases all resources used by the XmlTextReader.

Remarks

The XmlTextReader class is designed for high performance and efficient parsing of XML documents. It provides a forward-only, read-only view of the XML data.

Key features include:

  • Fast, sequential access to XML nodes.
  • Support for various encoding types.
  • Ability to read from different data sources (streams, files, URLs).
  • Provides access to node properties such as NodeType, Name, and Value.

For scenarios requiring more advanced XML processing, such as querying, editing, or navigating the document tree, consider using the XmlDocument class or LINQ to XML.

Examples

Reading XML from a String

using System;
using System.Xml;

public class Example
{
    public static void Main(string[] args)
    {
        string xmlString = @"
            
                Apple
                Banana
            ";

        using (XmlTextReader reader = new XmlTextReader(new System.IO.StringReader(xmlString)))
        {
            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Element:
                        Console.Write($"<{reader.Name}");
                        while (reader.MoveToNextAttribute())
                        {
                            Console.Write($" {reader.Name}='{reader.Value}'");
                        }
                        Console.WriteLine(">");
                        if (reader.HasValue)
                        {
                            Console.WriteLine($"  Value: {reader.Value}");
                        }
                        break;
                    case XmlNodeType.Text:
                        Console.WriteLine($"  Text: {reader.Value}");
                        break;
                    case XmlNodeType.EndElement:
                        Console.WriteLine($"");
                        break;
                }
            }
        }
    }
}

Reading XML from a File

using System;
using System.Xml;
using System.IO;

public class FileExample
{
    public static void Main(string[] args)
    {
        string filePath = "mydata.xml"; // Assume mydata.xml exists

        // Create a dummy XML file for demonstration
        File.WriteAllText(filePath, @"
            
                
                    Gambardella, Matthew
                    XML Developer's Guide
                    Computer
                    44.95
                
            ");

        try
        {
            using (XmlTextReader reader = new XmlTextReader(filePath))
            {
                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element && reader.Name == "title")
                    {
                        reader.Read(); // Move to the text node
                        Console.WriteLine($"Book Title: {reader.Value}");
                    }
                }
            }
        }
        catch (FileNotFoundException)
        {
            Console.WriteLine($"Error: File not found at {filePath}");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"An error occurred: {ex.Message}");
        }
        finally
        {
            // Clean up the dummy file
            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }
        }
    }
}

Inheritance

System.Object > System.MarshalByRefObject > System.Xml.XmlReader > System.Xml.XmlTextReader

Implements

  • System.IDisposable