Export (0) Print
Expand All

XmlNode.SelectSingleNode Method (String)

Selects the first XmlNode that matches the XPath expression.

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

public XmlNode SelectSingleNode (
	string xpath
)
public XmlNode SelectSingleNode (
	String xpath
)
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. The XmlNode 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 XmlNode, and vice versa.

Exception typeCondition

XPathException

The XPath expression contains a prefix.

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.

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

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

The following example changes the price of the first Jane Austen book.

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

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

        XmlNode book;
        XmlNode root = doc.get_DocumentElement();

        book = 
            root.SelectSingleNode("descendant::book[author/last-name='Austen']");

        //Change the price on the book.
        book.get_LastChild().set_InnerText("15.95");

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