Selects the first XmlNode that matches the XPath expression. Any prefixes found in the XPath expression are resolved using the supplied XmlNamespaceManager.
Namespace:
System.Xml
Assembly:
System.Xml (in System.Xml.dll)
Visual Basic (Declaration)
Public Function SelectSingleNode ( _
xpath As String, _
nsmgr As XmlNamespaceManager _
) As XmlNode
Dim instance As XmlNode
Dim xpath As String
Dim nsmgr As XmlNamespaceManager
Dim returnValue As XmlNode
returnValue = instance.SelectSingleNode(xpath, _
nsmgr)
public XmlNode SelectSingleNode(
string xpath,
XmlNamespaceManager nsmgr
)
public:
XmlNode^ SelectSingleNode(
String^ xpath,
XmlNamespaceManager^ nsmgr
)
public function SelectSingleNode(
xpath : String,
nsmgr : XmlNamespaceManager
) : XmlNode
Return Value
Type:
System.Xml..::.XmlNodeThe first XmlNode that matches the XPath query or nullNothingnullptra 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 | Condition |
|---|
| XPathException | The XPath expression contains a prefix which is not defined in the XmlNamespaceManager. |
XPath expressions can include namespaces. Namespace resolution is supported using the XmlNamespaceManager. If the XPath expression includes a prefix, the prefix and namespace URI pair must be added to the XmlNamespaceManager.
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 add a prefix and namespace URI to the XmlNamespaceManager; otherwise, you will not get a node selected. For more information, see Select Nodes Using XPath Navigation. |
For example, if you had the following XML:
<bookstore xmlns="http://www.lucernepublishing.com">
<book>
<title>Pride And Prejudice</title>
</book>
</bookstore>
The following C# code selects the first book node:
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("ab", "http://www.lucernepublishing.com");
XmlNode book = doc.SelectSingleNode("//ab:book", nsmgr);
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:
<bookstore xmlns="http://www.lucernepublishing.com">
<book>
<title>'Emma'</title>
</book>
</bookstore>
The following Visual Basic code selects an element that contains single quotes:
Dim nsmgr As XmlNamespaceManager = New XmlNamespaceManager(doc.NameTable)
nsmgr.AddNamespace("ab", "http://www.lucernepublishing.com")
book = root.SelectSingleNode("descendant::ab:book[ab:title=""'Emma'""]", nsmgr)
This method is a Microsoft extension to the Document Object Model (DOM).
The following example selects the book with the matching ISBN value.
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
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);
}
}
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
XmlDocument^ doc = gcnew XmlDocument;
doc->Load( "booksort.xml" );
//Create an XmlNamespaceManager for resolving namespaces.
XmlNamespaceManager^ nsmgr = gcnew 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->Item[@bk:ISBN='1-861001-57-6']", nsmgr );
Console::WriteLine( book->OuterXml );
}
#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>
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
.NET Framework
Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
.NET Compact Framework
Supported in: 3.5, 2.0
XNA Framework
Supported in: 3.0, 2.0, 1.0
Reference