SelectNodes Method (String)
Collapse the table of content
Expand the table of content

XmlNode.SelectNodes Method (String)


Selects a list of nodes matching the XPath expression.

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

public XmlNodeList SelectNodes(
	string xpath


Type: System.String

The XPath expression.

Return Value

Type: System.Xml.XmlNodeList

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

Exception Condition

The XPath expression contains a prefix. See XPath Examples.

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:


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 all books where the author's last name is Austen, and then changes the price of those books.

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....");


This example uses the following XML:

<?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>

.NET Framework
Available since 1.1
Return to top
© 2016 Microsoft