.NET Framework Class Library
XmlNode..::.SelectNodes Method (String)

Selects a list of nodes matching the XPath expression.

Namespace:  System.Xml
Assembly:  System.Xml (in System.Xml.dll)
Syntax

Visual Basic (Declaration)
Public Function SelectNodes ( _
    xpath As String _
) As XmlNodeList
Visual Basic (Usage)
Dim instance As XmlNode
Dim xpath As String
Dim returnValue As XmlNodeList

returnValue = instance.SelectNodes(xpath)
C#
public XmlNodeList SelectNodes(
    string xpath
)
Visual C++
public:
XmlNodeList^ SelectNodes(
    String^ xpath
)
JScript
public function SelectNodes(
    xpath : String
) : XmlNodeList

Parameters

xpath
Type: System..::.String
The XPath expression.

Return Value

Type: System.Xml..::.XmlNodeList
An XmlNodeList containing a collection of nodes matching the XPath query. The XmlNodeList should not be expected to be connected "live" to the XML document. That is, changes that appear in the XML document may not appear in the XmlNodeList, and vice versa.
Exceptions

ExceptionCondition
XPathException

The XPath expression contains a prefix.

Remarks

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.

NoteNote:

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.

NoteNote:

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>&apos;Emma&apos;</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).

Examples

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()
    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
C#
using System;
using System.IO;
using System.Xml;

public class Sample {

  public static void Main() {

    XmlDocument doc = new XmlDocument();
    doc.Load("booksort.xml");

    XmlNodeList nodeList;
    XmlNode root = doc.DocumentElement;

    nodeList=root.SelectNodes("descendant::book[author/last-name='Austen']");

    //Change the price on the books.
    foreach (XmlNode book in nodeList)
    {
      book.LastChild.InnerText="15.95";
    }

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

  }
}
Visual C++
#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   XmlDocument^ doc = gcnew XmlDocument;
   doc->Load( "booksort.xml" );
   XmlNodeList^ nodeList;
   XmlNode^ root = doc->DocumentElement;
   nodeList = root->SelectNodes( "descendant::book[author/last-name='Austen']" );

   //Change the price on the books.
   System::Collections::IEnumerator^ myEnum = nodeList->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      XmlNode^ book = safe_cast<XmlNode^>(myEnum->Current);
      book->LastChild->InnerText = "15.95";
   }

   Console::WriteLine( "Display the modified XML document...." );
   doc->Save( Console::Out );
}

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

int main()
{
    XmlDocument* doc = new XmlDocument();
    doc->Load(S"booksort.xml");

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

    nodeList=root->SelectNodes(S"descendant::book[author/last-name='Austen']");

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

    Console::WriteLine(S"Display the modified XML document....");
    doc->Save(Console::Out);
}

The example uses the file, booksort.xml, as input.

None
<?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>
    <author>
      <first-name>Jane</first-name>
      <last-name>Austen</last-name>
    </author>
    <price>24.95</price>
  </book>
  <book genre="novel" publicationdate="1992" bk:ISBN="1-861002-30-1">
    <title>The Handmaid's Tale</title>
    <author>
      <first-name>Margaret</first-name>
      <last-name>Atwood</last-name>
    </author>
    <price>29.95</price>
  </book>
  <book genre="novel" publicationdate="1991" bk:ISBN="1-861001-57-6">
    <title>Emma</title>
    <author>
      <first-name>Jane</first-name>
      <last-name>Austen</last-name>
    </author>
    <price>19.95</price>
  </book>
  <book genre="novel" publicationdate="1982" bk:ISBN="1-861001-45-3">
    <title>Sense and Sensibility</title>
    <author>
      <first-name>Jane</first-name>
      <last-name>Austen</last-name>
    </author>
    <price>19.95</price>
  </book>
</bookstore>
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
See Also

Reference

Tags :


Community Content

Michael Freidgeim
SelectNodes returns empty XmlNodeList if no matching node is found?
Is it the valid statement:
"SelectNodes returns empty XmlNodeList if no matching node is found"?
It's worth to clarify it in the help.

Note that for XmlNode..::.SelectSingleNode it is documented that it returns null reference (Nothing in Visual Basic) if no matching node is found.
Tags : contentbug

artegol
Version of XPath
What version of XPath can be used as xpath parameter? Does Microsoft implementation differs from that version of specification?
Tags :

MollyBos - MSFT
See Also

Select Nodes Using XPath Navigation: http://msdn.microsoft.com/en-us/library/d271ytdx.aspx
XPath Reference: http://msdn.microsoft.com/en-us/library/ms256115.aspx

Tags :

Page view tracker