XPathReader Class
The XPathReader class provides a way to read XML documents using XPath expressions.
It extends the functionality of standard XML readers by allowing you to query for specific nodes
or sets of nodes directly.
Introduction
When working with XML data in Windows applications, it's often necessary to extract specific pieces
of information without parsing the entire document manually. The XPathReader simplifies
this process by integrating the power of XPath queries with efficient XML reading capabilities.
Syntax
public class XPathReader : XmlReader
{
// Constructors
public XPathReader(XmlReader reader, string xpath);
public XPathReader(Stream stream, string xpath);
public XPathReader(string uri, string xpath);
// Methods
public override bool Read();
public override string GetAttribute(int i);
public override string GetAttribute(string name);
public override string GetAttribute(string localName, string namespaceURI);
// Properties
public override int AttributeCount { get; }
public override string BaseURI { get; }
public override int Depth { get; }
public override bool EOF { get; }
public override string GetValue();
public override bool IsEmptyElement { get; }
public override string LocalName { get; }
public override string Name { get; }
public override string NamespaceURI { get; }
public override XmlNameTable NameTable { get; }
public override XmlNodeType NodeType { get; }
public override string Prefix { get; }
public override ReadState ReadState { get; }
public override string Value { get; }
public override XmlReaderSettings Settings { get; }
public override string XmlLang { get; }
public override string XmlSpace { get; }
// XPath specific methods (conceptual, actual API might differ)
public XPathNodeIterator Select(string xpath);
public object Evaluate(string xpath);
}
Constructors
| Constructor | Description |
|---|---|
XPathReader(XmlReader reader, string xpath) |
Initializes a new instance of the XPathReader class that reads from the specified XmlReader and applies the given XPath expression. |
XPathReader(Stream stream, string xpath) |
Initializes a new instance of the XPathReader class that reads from the specified stream and applies the given XPath expression. |
XPathReader(string uri, string xpath) |
Initializes a new instance of the XPathReader class that reads from the specified URI and applies the given XPath expression. |
Methods
The XPathReader overrides several methods from the base XmlReader class to provide its functionality. Key methods include:
Read(): Advances the reader to the next node that matches the XPath expression.GetAttribute(): Retrieves the value of an attribute.Select(string xpath): Selects a node set based on the provided XPath expression. Returns anXPathNodeIterator.Evaluate(string xpath): Evaluates an XPath expression and returns its result.
Properties
The XPathReader exposes properties inherited from XmlReader, such as NodeType, LocalName, Value, and Depth, which describe the current node being read.
XPath Functionality
The core feature of XPathReader is its ability to filter XML nodes based on an XPath query provided during instantiation. When you call Read(), the reader advances only to nodes that satisfy the initial XPath expression. You can also use the Select() method to perform further queries on the document.
XPathReader is designed to simplify reading specific parts of an XML document. For complex transformations or modifications, consider using other XML processing APIs like XmlDocument or LINQ to XML.
Example Usage
C# Example
Reading all <book> elements from an XML file:
using System.Xml;
using System.IO;
// Assuming 'myDocument.xml' contains an XML structure with book elements
string xmlFilePath = "myDocument.xml";
string xpathExpression = "//book"; // Select all 'book' elements anywhere in the document
try
{
using (XmlReader baseReader = XmlReader.Create(xmlFilePath))
{
using (XPathReader xpathReader = new XPathReader(baseReader, xpathExpression))
{
while (xpathReader.Read())
{
if (xpathReader.NodeType == XmlNodeType.Element && xpathReader.LocalName == "book")
{
Console.WriteLine($"Found book: {xpathReader.GetAttribute("title")}");
// You can further navigate within the book element using the xpathReader
}
}
}
}
}
catch (XmlException ex)
{
Console.WriteLine($"Error reading XML: {ex.Message}");
}
Using Select() for further queries
// Continuing from the previous example
using (XPathReader xpathReader = new XPathReader(xmlFilePath, "//catalog/book"))
{
// Select all 'author' elements within 'book' elements in the 'catalog'
XPathNodeIterator authorsIterator = xpathReader.Select("//catalog/book/author");
while (authorsIterator.MoveNext())
{
Console.WriteLine($"Author: {authorsIterator.Current.Value}");
}
}