XmlNode.SelectNodes Method (String)
Selects a list of nodes matching the XPath expression.
Assembly: System.Xml (in System.Xml.dll)
If the XPath expression requires namespace resolution, you must use the SelectNodes overload which takes an XmlNamespaceManager as its argument. The XmlNamespaceManager is used to resolve namespaces.
If the XPath expression 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 XmlNamespaceManager and add a prefix and namespace URI to it; otherwise, you will not get any nodes selected. For more information, see Select Nodes Using XPath Navigation.
A common issue when formulating XPath expressions is how to include a single quote (') or double quote (") in the expression. If you have to search for a value that includes a single quote, you must enclose the string in double quotes. If you need to search for a value that includes a double quote, you must enclose the string in single quotes.
For example, suppose you have the following XML:
<bookstore> <book> <title>'Emma'</title> </book> </bookstore>
The following Visual Basic code selects an element that contains single quotes:
nodeList = root.SelectNodes("//book[contains(title,""'Emma'"")]")
This method is a Microsoft extension to the Document Object Model (DOM).
The XmlNodeList object returned by this method will be valid while the underlying document remains unchanged. If the underlying document changes, unexpected results may be returned (no exception will be thrown).
The following example selects and displays all book titles.
Imports System Imports System.IO Imports System.Xml public class Sample public shared sub Main() 'Create the XmlDocument. Dim doc as XmlDocument = new XmlDocument() doc.Load("booksort.xml") Dim book as XmlNode Dim nodeList as XmlNodeList Dim root as XmlNode = doc.DocumentElement nodeList=root.SelectNodes("descendant::book[author/last-name='Austen']") 'Change the price on the books. for each book in nodeList book.LastChild.InnerText="15.95" next Console.WriteLine("Display the modified XML document....") doc.Save(Console.Out) end sub end class
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.