This documentation is archived and is not being maintained.

XmlNode.SelectNodes Method (String)

.NET Framework 1.1

Selects a list of nodes matching the XPath expression.

[Visual Basic]
Overloads Public Function SelectNodes( _
   ByVal xpath As String _
) As XmlNodeList
public XmlNodeList SelectNodes(
 string xpath
public: XmlNodeList* SelectNodes(
 String* xpath
public function SelectNodes(
   xpath : String
) : XmlNodeList;


The XPath expression.

Return Value

An XmlNodeList containing a collection of nodes matching the XPath query.


Exception Type Condition
XPathException The XPath expression contains a prefix.


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.

Note   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.

This method is a Microsoft extension to the Document Object Model (DOM).


[Visual Basic, C#, C++] The following example changes the price on all books by Jane Austen.

[Visual Basic] 
Imports System
Imports System.IO
Imports System.Xml

public class Sample

  public shared sub Main()

    'Create the XmlDocument.
    Dim doc as XmlDocument = new XmlDocument()
    Dim book as XmlNode
    Dim nodeList as XmlNodeList 
    Dim root as XmlNode = doc.DocumentElement

    'Change the price on the books.
    for each book in nodeList      

    Console.WriteLine("Display the modified XML document....")
  end sub
end class

using System;
using System.IO;
using System.Xml;

public class Sample {

  public static void Main() {

    XmlDocument doc = new XmlDocument();

    XmlNodeList nodeList;
    XmlNode root = doc.DocumentElement;

    //Change the price on the books.
    foreach (XmlNode book in nodeList)

    Console.WriteLine("Display the modified XML document....");

#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;

int main()
    XmlDocument* doc = new XmlDocument();

    XmlNodeList* nodeList;
    XmlNode* root = doc->DocumentElement;

    //Change the price on the books.
    System::Collections::IEnumerator* myEnum = nodeList->GetEnumerator();
    while (myEnum->MoveNext())
        XmlNode* book = __try_cast<XmlNode*>(myEnum->Current);

    Console::WriteLine(S"Display the modified XML document....");

[Visual Basic, C#, C++] The example uses the file, booksort.xml, as input.

<?xml version="1.0"?>
<!-- A fragment of a book store inventory database -->
<bookstore xmlns:bk="urn:samples">
  <book genre="novel" publicationdate="1997" bk:ISBN="1-861001-57-8">
    <title>Pride And Prejudice</title>
  <book genre="novel" publicationdate="1992" bk:ISBN="1-861002-30-1">
    <title>The Handmaid's Tale</title>
  <book genre="novel" publicationdate="1991" bk:ISBN="1-861001-57-6">
  <book genre="novel" publicationdate="1982" bk:ISBN="1-861001-45-3">
    <title>Sense and Sensibility</title>

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

XmlNode Class | XmlNode Members | System.Xml Namespace | XmlNode.SelectNodes Overload List | SelectSingleNode