XPointer Class
The XPointer class provides an implementation of the XPointer language for locating fragments within an XML document using a set of XPath expressions. This class is part of the System.Xml.Linq namespace and resides in the System.Xml.Linq.dll assembly.
Namespace
System.Xml.Linq
Assembly
System.Xml.Linq.dll
Syntax
public sealed class XPointer
Constructors
| Constructor | Summary |
|---|---|
XPointer(string pointer) |
Initializes a new instance of the XPointer class using the supplied XPointer expression. |
Methods
| Method | Returns | Summary |
|---|---|---|
bool TryResolve(XElement element, out IEnumerable<XNode> nodes) |
bool | Attempts to resolve the XPointer expression against the specified element and returns the matching nodes. |
bool TryResolve(XDocument document, out IEnumerable<XNode> nodes) |
bool | Attempts to resolve the XPointer expression against the specified document. |
Properties
| Property | Type | Summary |
|---|---|---|
string Expression |
string | Gets the XPointer expression used to locate nodes. |
Examples
// Sample usage of XPointer
using System;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Xml.Linq.XPath;
class Program
{
static void Main()
{
XDocument doc = XDocument.Load("Books.xml");
// XPointer expression that selects the second book element
string pointer = "element(/bookstore/book[2])";
XPointer xptr = new XPointer(pointer);
if (xptr.TryResolve(doc, out IEnumerable<XNode> nodes))
{
foreach (var node in nodes)
{
Console.WriteLine(node);
}
}
else
{
Console.WriteLine("No nodes matched the XPointer.");
}
}
}