Export (0) Print
Expand All

XmlNode.SelectNodes Method

Selects a list of nodes matching the XPath expression.

Overload List

Selects a list of nodes matching the XPath expression.

[Visual Basic] Overloads Public Function SelectNodes(String) As XmlNodeList
[C#] public XmlNodeList SelectNodes(string);
[C++] public: XmlNodeList* SelectNodes(String*);
[JScript] public function SelectNodes(String) : XmlNodeList;

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

[Visual Basic] Overloads Public Function SelectNodes(String, XmlNamespaceManager) As XmlNodeList
[C#] public XmlNodeList SelectNodes(string, XmlNamespaceManager);
[C++] public: XmlNodeList* SelectNodes(String*, XmlNamespaceManager*);
[JScript] public function SelectNodes(String, XmlNamespaceManager) : XmlNodeList;

Example

[Visual Basic, C#, C++] The following example displays the values of each of the ISBN attributes.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of SelectNodes. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Imports System
Imports System.IO
Imports System.Xml

public class Sample

  public shared sub Main()

      Dim doc as XmlDocument = new XmlDocument()
      doc.Load("booksort.xml")

      'Create an XmlNamespaceManager for resolving namespaces.
      Dim nsmgr as XmlNamespaceManager = new XmlNamespaceManager(doc.NameTable)
      nsmgr.AddNamespace("bk", "urn:samples")

      'Select and display the value of all the ISBN attributes.
      Dim nodeList as XmlNodeList 
      Dim root as XmlElement = doc.DocumentElement
      nodeList = root.SelectNodes("/bookstore/book/@bk:ISBN", nsmgr)
      Dim isbn as XmlNode
      for each isbn in nodeList
        Console.WriteLine(isbn.Value)
      next

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

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

   }

}

[C++] 
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Collections;

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

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

   // Select and display the value of all the ISBN attributes.
   XmlNodeList * nodeList;
   XmlElement * root = doc -> DocumentElement;
   nodeList = root -> SelectNodes(S"/bookstore/book/@bk:ISBN", nsmgr);
   IEnumerator* myEnum = nodeList->GetEnumerator();
   while (myEnum->MoveNext())
   {
      XmlNode* isbn = __try_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>
    <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>

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

See Also

XmlNode Class | XmlNode Members | System.Xml Namespace

Show:
© 2014 Microsoft