IXmlReader Interface
Represents a reader that provides forward-only, read-only access to an XML document. This interface is the core component for parsing and navigating XML data efficiently without loading the entire document into memory.
Namespace
System.Xml
Summary of Members
The IXmlReader
interface defines a set of methods and properties that allow you to:
- Read the next node in the XML document.
- Determine the type of the current node (element, attribute, text, etc.).
- Retrieve the name and value of the current node.
- Navigate through the XML tree structure.
- Handle namespaces and their prefixes.
- Check for the end of the document or elements.
- Access attributes of elements.
Key Properties
NodeType
: Gets the type of the current node.Name
: Gets the qualified name of the current node.LocalName
: Gets the local 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.NamespaceURI
: Gets the Namespace URI (as defined in XML-Data (XSD)) of the node on which the reader is positioned.Prefix
: Gets the namespace prefix associated with the current node.IsEmptyElement
: Gets a value indicating whether the current node is an empty element tag (e.g.,<MyElement/>
).
Key Methods
Read()
: Moves the reader to the next node in the XML document.MoveToAttribute(int index)
: Moves the reader to the attribute with the specified index.MoveToAttribute(string name)
: Moves the reader to the attribute with the specified name.MoveToElement()
: Moves the reader to the element that contains the current attribute.GetAttribute(int index)
: Gets the value of the attribute at the specified index.GetAttribute(string name)
: Gets the value of the attribute with the specified name.ReadAttributeValue()
: Reads the text of an attribute and moves the reader past it.ReadStartElement()
: Reads the start tag of a given element and moves the reader past the start tag.ReadEndElement()
: Reads the end tag of a given element and moves the reader past the end tag.Skip()
: Skips the current node and its children.
Example Usage
The following C# code demonstrates how to use IXmlReader
to read elements and attributes from an XML string:
using System;
using System.Xml;
public class XmlReaderExample
{
public static void Main(string[] args)
{
string xmlString = @"<bookstore>
<book category=""fiction"">
<title lang=""en"">The Great Gatsby</title>
<author>F. Scott Fitzgerald</author>
<year>1925</year>
<price>10.99</price>
</book>
<book category=""science"">
<title lang=""en"">A Brief History of Time</title>
<author>Stephen Hawking</author>
<year>1988</year>
<price>15.50</price>
</book>
</bookstore>";
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(reader.IsEmptyElement ? "/>" : ">");
break;
case XmlNodeType.Text:
Console.WriteLine($" {reader.Value}");
break;
case XmlNodeType.EndElement:
Console.WriteLine($"</{reader.Name}>");
break;
}
}
}
}
}
See Also
- XmlReader Class (The concrete implementation of
IXmlReader
) - System.Xml Namespace