Implements a non-cached, forward-only reader as an XmlReader and provides direct access to the source document.

This class provides a fast, forward-only, read-only way to read an XML stream or URI. It is designed for scenarios where memory usage is a concern and the entire XML document does not need to be loaded into memory. It is the recommended way to read XML unless you need the features provided by XmlDocument.

Public Constructors

Public Methods

Public Properties

Example

using System;
using System.Xml;

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

  
    Gambardella, Matthew
    XML Developer's Guide
    Computer
    44.95
    2000-10-01
    An in-depth look at creating applications with XML.
  
  
    Ralls, Kim
    Midnight Rain
    Fantasy
    5.95
    2000-12-16
    A former architect battles corporate zombies,
      an evil sorceress, and her own childhood to become queen
      of the world.
  
";

        using (XmlTextReader reader = new XmlTextReader(new System.IO.StringReader(xmlString)))
        {
            reader.WhitespaceHandling = WhitespaceHandling.None; // Ignore whitespace

            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Element:
                        Console.Write($"<{reader.Name}");
                        while (reader.MoveToNextAttribute())
                        {
                            Console.Write($" {reader.Name}=\"{reader.Value}\"");
                        }
                        Console.WriteLine(">");
                        break;
                    case XmlNodeType.Text:
                        Console.WriteLine($"  {reader.Value}");
                        break;
                    case XmlNodeType.EndElement:
                        Console.WriteLine($"");
                        break;
                }
            }
        }
    }
}