Selects a node set using the specified System.Xml.XPath.XPathExpression.
- expr
- An System.Xml.XPath.XPathExpression object containing the compiled XPath query.
An System.Xml.XPath.XPathNodeIterator that points to the selected node set.
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.
You can still use any of the System.Xml.XPath.XPathNavigator object's navigation methods to move within the System.Xml.XPath.XPathNavigator. The System.Xml.XPath.XPathNavigator navigation methods are independent of the selected nodes in the System.Xml.XPath.XPathNodeIterator.
Future calls to the XPathNavigator.Select(string) method return a new System.Xml.XPath.XPathNodeIterator object that points to the selected set of nodes that matches the new XPathNavigator.Select(string) call. The two System.Xml.XPath.XPathNodeIterator objects are completely independent of each other.
If the System.Xml.XPath.XPathExpression requires namespace resolution, the prefix and namespace URI pair must be added to an System.Xml.XmlNamespaceManager, and the XPathExpression.SetContext(System.Xml.XmlNamespaceManager) method must be called to specify the System.Xml.XmlNamespaceManager to use for namespace resolution.
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);
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.