Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
SelectNodes Method (String, XmlNamespaceManager)
Collapse the table of content
Expand the table of content

XmlNode::SelectNodes Method (String, XmlNamespaceManager)

Selects a list of nodes matching the XPath expression. Any prefixes found in the XPath expression are resolved using the supplied XmlNamespaceManager.

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

XmlNodeList^ SelectNodes(
	String^ xpath, 
	XmlNamespaceManager^ nsmgr


Type: System::String

The XPath expression. See XPath Examples.

Type: System.Xml::XmlNamespaceManager

An XmlNamespaceManager to use for resolving namespaces for prefixes in the XPath expression.

Return Value

Type: System.Xml::XmlNodeList
An XmlNodeList containing a collection of nodes matching the XPath query.


The XPath expression contains a prefix which is not defined in the XmlNamespaceManager.

XPath expressions can include namespaces. Namespace resolution is supported using the XmlNamespaceManager. If the XPath expression includes a prefix, the prefix and namespace URI pair must be added to the XmlNamespaceManager.


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 add a prefix and namespace URI to the XmlNamespaceManager; otherwise, you will not get any nodes selected. For more information, see Select Nodes Using XPath Navigation.

For example, if you had the following XML:

 <bookstore xmlns="http://www.lucernepublishing.com">
    <title>Pride And Prejudice</title>

The following C# code selects all book nodes:

 XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
 nsmgr.AddNamespace("ab", "http://www.lucernepublishing.com");
 XmlNodeList nodelist = doc.SelectNodes("//ab:book", nsmgr);

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 xmlns="http://www.lucernepublishing.com">

The following Visual Basic code selects an element that contains single quotes:

 Dim nsmgr As XmlNamespaceManager = New XmlNamespaceManager(doc.NameTable)
 nsmgr.AddNamespace("ab", "http://www.lucernepublishing.com")
 nodeList = root.SelectNodes("//ab:book[contains(ab:title,""'Emma'"")]", nsmgr)

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 displays the values of each of the ISBN attributes. This example uses an XmlElement object, which inherits from the XmlNode class.

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Collections;
int main()
   XmlDocument^ doc = gcnew XmlDocument;
   doc->Load( "booksort.xml" );

   // Create an XmlNamespaceManager for resolving namespaces.
   XmlNamespaceManager^ nsmgr = gcnew XmlNamespaceManager( doc->NameTable );
   nsmgr->AddNamespace( "bk", "urn:samples" );

   // Select and display the value of all the ISBN attributes.
   XmlNodeList^ nodeList;
   XmlElement^ root = doc->DocumentElement;
   nodeList = root->SelectNodes( "/bookstore/book/@bk:ISBN", nsmgr );
   IEnumerator^ myEnum = nodeList->GetEnumerator();
   while ( myEnum->MoveNext() )
      XmlNode^ isbn = safe_cast<XmlNode^>(myEnum->Current);
      Console::WriteLine( isbn->Value );

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>

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0
© 2015 Microsoft