XPathNodeList Class
The XPathNodeList class provides a collection of nodes selected by an XPath query.
Namespace
System.Xml.XPath
Assembly
System.Xml.XPath.dll
Inheritance
Object → XPathNodeList
Properties
| Name | Type | Description |
|---|---|---|
Count |
int |
Gets the number of nodes in the collection. |
Item[int index] |
XPathNavigator |
Gets the node at the specified index. |
Methods
| Name | Return Type | Description |
|---|---|---|
GetEnumerator() |
IEnumerator |
Returns an enumerator that can iterate through the collection. |
Clone() |
XPathNodeList |
Creates a shallow copy of the XPathNodeList. |
Remarks
An XPathNodeList is typically returned by the Select method of an XPathNavigator or XPathExpression. The collection is read‑only; attempts to modify it will cause a runtime exception.
Example
using System;
using System.Xml;
using System.Xml.XPath;
class Program
{
static void Main()
{
string xml = @"<books>
<book genre='novel'>
<title>The Handmaid's Tale</title>
</book>
<book genre='poetry'>
<title>Leaves of Grass</title>
</book>
</books>";
XPathDocument doc = new XPathDocument(new System.IO.StringReader(xml));
XPathNavigator nav = doc.CreateNavigator();
XPathNodeList nodes = nav.Select("//book[@genre='novel']/title");
foreach (XPathNavigator title in nodes)
{
Console.WriteLine(title.Value);
}
}
}