System.Xml.XmlNode.SelectSingleNode Method

Selects the first XmlNode that matches the XPath expression.

Syntax

public XmlNode SelectSingleNode (string xpath)

Parameters

xpath
The XPath expression.

Returns

The first XmlNode that matches the XPath query or null 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.

Remarks

If the XPath expression requires namespace resolution, you must use the SelectSingleNode overload which takes an System.Xml.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. For more information, see Select Nodes Using XPath Navigation.

Note:

A common issue when formulating XPath expressions is how to include a single quote (') or double quote (") in the expression. If you have to search for a value that includes a single quote, you must enclose the string in double quotes. If you need to search for a value that includes a double quote, you must enclose the string in single quotes.

For example, suppose you have the following XML:

Example

 <bookstore>
   <book>
     <title>&apos;Emma&apos;</title>
   </book>
 </bookstore>

The following Visual Basic code selects an element that contains single quotes:

Example

 book = root.SelectSingleNode("descendant::book[title=""'Emma'""]")

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

Requirements

Namespace: System.Xml
Assembly: System.Xml (in System.Xml.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0