Export (0) Print
Expand All

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)

public XmlNodeList SelectNodes (
	string xpath,
	XmlNamespaceManager nsmgr
)
public XmlNodeList SelectNodes (
	String xpath, 
	XmlNamespaceManager nsmgr
)
public function SelectNodes (
	xpath : String, 
	nsmgr : XmlNamespaceManager
) : XmlNodeList

Parameters

xpath

The XPath expression.

nsmgr

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

Return Value

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.

Exception typeCondition

XPathException

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.

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 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">
  <book>
    <title>Pride And Prejudice</title>
  </book>
 </bookstore>

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

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

The following example displays the values of each of the ISBN attributes.

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

public class Sample
{
  public static void Main()
  {

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

      //Create an XmlNamespaceManager for resolving namespaces.
      XmlNamespaceManager nsmgr = new 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);
      foreach (XmlNode isbn in nodeList){
        Console.WriteLine(isbn.Value);
      }

   }

}

import System.*;
import System.IO.*;
import System.Xml.*;

public class Sample
{
    public static void main(String[] args)
    {
        XmlDocument doc = new XmlDocument();
        doc.Load("booksort.xml");
        //Create an XmlNamespaceManager for resolving namespaces.
        XmlNamespaceManager nsMgr = new XmlNamespaceManager(
            doc.get_NameTable());
        nsMgr.AddNamespace("bk", "urn:samples");
        //Select and display the value of all the ISBN attributes.
        XmlNodeList nodeList;
        XmlElement root = doc.get_DocumentElement();
        nodeList = root.SelectNodes("/bookstore/book/@bk:ISBN", nsMgr);
        for (int iCtr = 0; iCtr < nodeList.get_Count(); iCtr++) {
            XmlNode isbn = nodeList.get_ItemOf(iCtr);
            Console.WriteLine(isbn.get_Value());
        }
    } //main 
} //Sample

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

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft