System.Xml.XPath.XPathNavigator.Select Method

Selects a node set using the specified System.Xml.XPath.XPathExpression.

Syntax

public virtual XPathNodeIterator Select (XPathExpression expr)

Parameters

expr
An System.Xml.XPath.XPathExpression object containing the compiled XPath query.

Returns

An System.Xml.XPath.XPathNodeIterator that points to the selected node set.

Remarks

The context for the selection is the position of the System.Xml.XPath.XPathNavigator when you called this method. After you call this method, the System.Xml.XPath.XPathNodeIterator returned represents the set of selected nodes. Use XPathNodeIterator.MoveNext on the System.Xml.XPath.XPathNodeIterator to iterate over the selected node set.

The following C# code iterates over the selected set of nodes.

Example

XPathNodeIterator ni = nav.Select(expr);
while (ni.MoveNext())
{
    Console.WriteLine(ni.Current.Name);
}

The following are important notes to consider when using the XPathNavigator.Select(string) method.

For example, suppose the document contains the following XML nodes.

Example

<bookstore xmlns:bk='urn:samples'>
    <book bk:ISBN='1-325-0980'>
        <title>Pride And Prejudice</title>
    </book>
</bookstore>

In this case, the following C# code selects the bk:ISBN node.

Example

XPathExpression expr = nav.Compile("book/@bk:ISBN");
XmlNamespaceManager mngr = new XmlNamespaceManager(new NameTable());
mngr.AddNamespace("bk","urn:samples");
expr.SetContext(mngr);
XPathNodeIterator ni = nav.Select(expr);
Note:

If the System.Xml.XPath.XPathExpression does not include a prefix, it is assumed that the namespace URI is the empty namespace. If your XML includes a default namespace, you must still use the XPathExpression.SetContext(System.Xml.XmlNamespaceManager) method and provide an System.Xml.XmlNamespaceManager that contains a prefix and namespace URI to handle the default namespace.

For example, suppose you have the following XML.

Example

<bookstore xmlns="http://www.lucernepublishing.com">
    <book>
        <title>Pride And Prejudice</title>
    </book>
</bookstore>

In this case, the following C# code selects all book nodes:

Example

XmlNamespaceManager nsmgr = new XmlNamespaceManager(nav.NameTable);
nsmgr.AddNamespace("ab", "http://www.lucernepublishing.com");
XPathExpression expr;
expr = nav.Compile("//ab:book");
expr.SetContext(nsmgr);
XPathNodeIterator ni = nav.Select(expr);

This method has no effect on the state of the System.Xml.XPath.XPathNavigator.

Requirements

Namespace: System.Xml.XPath
Assembly: System.Xml (in System.Xml.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0