Export (0) Print
Expand All

XmlNode.SelectSingleNode Method (String)

.NET Framework 1.1

Selects the first XmlNode that matches the XPath expression.

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

Parameters

xpath
The XPath expression.

Return Value

The first XmlNode that matches the XPath query or a null reference (Nothing in Visual Basic) if no matching node is found.

Exceptions

Exception Type Condition
XPathException The XPath expression contains a prefix.

Remarks

If the XPath expression requires namespace resolution, you must use the SelectSingleNode overload which takes an XmlNamespaceManager as its argument. The XmlNamespaceManager is used to resolve namespaces.

Note   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 a selected node.

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

Example

[Visual Basic, C#, C++] The following example changes the price of the first Jane Austen book.

[Visual Basic] 
Imports System
Imports System.IO
Imports System.Xml

public class Sample

  public shared sub Main()

    'Create the XmlDocument.
    Dim doc as XmlDocument = new XmlDocument()
    doc.Load("booksort.xml")
           
    Dim book as XmlNode
    Dim root as XmlNode = doc.DocumentElement

    book=root.SelectSingleNode("descendant::book[author/last-name='Austen']")
 
    'Change the price on the book.
    book.LastChild.InnerText="15.95"

    Console.WriteLine("Display the modified XML document....")
    doc.Save(Console.Out)
    
  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");

    XmlNode book;
    XmlNode root = doc.DocumentElement;

    book=root.SelectSingleNode("descendant::book[author/last-name='Austen']");
 
    //Change the price on the book.
    book.LastChild.InnerText="15.95";

    Console.WriteLine("Display the modified XML document....");
    doc.Save(Console.Out);    
  }
}

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

    XmlNode* book;
    XmlNode* root = doc->DocumentElement;

    book=root->SelectSingleNode(S"descendant::book[author/last-name='Austen']");
 
    //Change the price on the book.
    book->LastChild->InnerText=S"15.95";

    Console::WriteLine(S"Display the modified XML document....");
    doc->Save(Console::Out);    
}

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.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

XmlNode Class | XmlNode Members | System.Xml Namespace | XmlNode.SelectSingleNode Overload List | SelectNodes

Show:
© 2014 Microsoft