This documentation is archived and is not being maintained.

XmlNode.SelectSingleNode Method

Selects the first XmlNode that matches the XPath expression.

Overload List

Selects the first XmlNode that matches the XPath expression.

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

Selects the first XmlNode that matches the XPath expression. Any prefixes found in the XPath expression are resolved using the supplied XmlNamespaceManager.

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

Example

[Visual Basic, C#, C++] The following example selects the book with the matching ISBN value.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of SelectSingleNode. 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 the book node with the matching attribute value.
      Dim book as XmlNode 
      Dim root as XmlElement = doc.DocumentElement
      book = root.SelectSingleNode("descendant::book[@bk:ISBN='1-861001-57-6']", nsmgr)

      Console.WriteLine(book.OuterXml)

  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 the book node with the matching attribute value.
      XmlNode book;
      XmlElement root = doc.DocumentElement;
      book = root.SelectSingleNode("descendant::book[@bk:ISBN='1-861001-57-6']", nsmgr);

      Console.WriteLine(book.OuterXml);

  }
}

[C++] 
#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");

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

   //Select the book node with the matching attribute value.
   XmlNode * book;
   XmlElement * root = doc -> DocumentElement;
   book = root -> SelectSingleNode(S"descendant::book->Item[@bk:ISBN='1-861001-57-6']", nsmgr);

   Console::WriteLine(book -> OuterXml);
}

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: