XPathNavigator.SelectSingleNode 方法
定义
选择 XPathNavigator 中的单个节点。Selects a single node in the XPathNavigator.
重载
| SelectSingleNode(String, IXmlNamespaceResolver) |
通过指定的 XPathNavigator 对象使用指定的 XPath 查询在 IXmlNamespaceResolver 中选择单个节点,以解析命名空间前缀。Selects a single node in the XPathNavigator object using the specified XPath query with the IXmlNamespaceResolver object specified to resolve namespace prefixes. |
| SelectSingleNode(String) |
使用指定的 XPath 查询选择 XPathNavigator 中的单个节点。Selects a single node in the XPathNavigator using the specified XPath query. |
| SelectSingleNode(XPathExpression) |
使用指定的 XPathNavigator 对象在 XPathExpression 中选择单个节点。Selects a single node in the XPathNavigator using the specified XPathExpression object. |
SelectSingleNode(String, IXmlNamespaceResolver)
通过指定的 XPathNavigator 对象使用指定的 XPath 查询在 IXmlNamespaceResolver 中选择单个节点,以解析命名空间前缀。Selects a single node in the XPathNavigator object using the specified XPath query with the IXmlNamespaceResolver object specified to resolve namespace prefixes.
public:
virtual System::Xml::XPath::XPathNavigator ^ SelectSingleNode(System::String ^ xpath, System::Xml::IXmlNamespaceResolver ^ resolver);
public virtual System.Xml.XPath.XPathNavigator? SelectSingleNode (string xpath, System.Xml.IXmlNamespaceResolver? resolver);
public virtual System.Xml.XPath.XPathNavigator SelectSingleNode (string xpath, System.Xml.IXmlNamespaceResolver resolver);
abstract member SelectSingleNode : string * System.Xml.IXmlNamespaceResolver -> System.Xml.XPath.XPathNavigator
override this.SelectSingleNode : string * System.Xml.IXmlNamespaceResolver -> System.Xml.XPath.XPathNavigator
Public Overridable Function SelectSingleNode (xpath As String, resolver As IXmlNamespaceResolver) As XPathNavigator
参数
- resolver
- IXmlNamespaceResolver
一个 IXmlNamespaceResolver 对象,该对象用于解析 XPath 查询中的命名空间前缀。The IXmlNamespaceResolver object used to resolve namespace prefixes in the XPath query.
返回
XPathNavigator 对象,其中包含指定的 XPath 查询的第一个匹配节点;如果没有查询结果,则为 null。An XPathNavigator object that contains the first matching node for the XPath query specified; otherwise null if there are no query results.
例外
XPath 查询中出错,或者 XPath 表达式的返回类型不是节点。An error was encountered in the XPath query or the return type of the XPath expression is not a node.
XPath 查询无效。The XPath query is not valid.
示例
下面的示例 XPathNavigator 基于指定的 XPath 查询从对象中选择单个节点,并使用指定的 XmlNamespaceManager 对象解析 XPath 查询中的命名空间前缀。The following example selects a single node from the XPathNavigator object based on the XPath query specified and uses the XmlNamespaceManager object specified to resolve namespace prefixes in the XPath query.
XPathDocument^ document = gcnew XPathDocument("contosoBooks.xml");
XPathNavigator^ navigator = document->CreateNavigator();
XmlNamespaceManager^ manager = gcnew XmlNamespaceManager(navigator->NameTable);
manager->AddNamespace("bk", "http://www.contoso.com/books");
XPathNavigator^ node = navigator->SelectSingleNode("//bk:title", manager);
Console::WriteLine(node->InnerXml);
XPathDocument document = new XPathDocument("contosoBooks.xml");
XPathNavigator navigator = document.CreateNavigator();
XmlNamespaceManager manager = new XmlNamespaceManager(navigator.NameTable);
manager.AddNamespace("bk", "http://www.contoso.com/books");
XPathNavigator node = navigator.SelectSingleNode("//bk:title", manager);
Console.WriteLine(node.InnerXml);
Dim document As XPathDocument = New XPathDocument("contosoBooks.xml")
Dim navigator As XPathNavigator = document.CreateNavigator()
Dim manager As XmlNamespaceManager = New XmlNamespaceManager(navigator.NameTable)
manager.AddNamespace("bk", "http://www.contoso.com/books")
Dim node As XPathNavigator = navigator.SelectSingleNode("//bk:title", manager)
Console.WriteLine(node.InnerXml)
该示例使用 contosoBooks.xml 文件作为输入。The example takes the contosoBooks.xml file as an input.
<?xml version="1.0" encoding="utf-8" ?>
<bookstore xmlns="http://www.contoso.com/books">
<book genre="autobiography" publicationdate="1981-03-22" ISBN="1-861003-11-0">
<title>The Autobiography of Benjamin Franklin</title>
<author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
<price>8.99</price>
</book>
<book genre="novel" publicationdate="1967-11-17" ISBN="0-201-63361-2">
<title>The Confidence Man</title>
<author>
<first-name>Herman</first-name>
<last-name>Melville</last-name>
</author>
<price>11.99</price>
</book>
<book genre="philosophy" publicationdate="1991-02-15" ISBN="1-861001-57-6">
<title>The Gorgias</title>
<author>
<name>Plato</name>
</author>
<price>9.99</price>
</book>
</bookstore>
注解
SelectSingleNode方法选择与指定的 XPath 查询匹配的第一个节点。The SelectSingleNode method selects the first node that matches the XPath query specified. SelectSingleNode方法使用 IXmlNamespaceResolver 对象解析命名空间前缀。The SelectSingleNode method uses the IXmlNamespaceResolver object to resolve namespace prefixes.
备注
在 XPath 中,所有命名空间都必须以查询为前缀。In XPath all namespaces must be prefixed in a query. 如果 XML 文档使用默认命名空间,则仍必须将命名空间前缀添加到 IXmlNamespaceResolver 对象。If an XML document uses a default namespace, you must still add a namespace prefix to the IXmlNamespaceResolver object.
适用于
SelectSingleNode(String)
使用指定的 XPath 查询选择 XPathNavigator 中的单个节点。Selects a single node in the XPathNavigator using the specified XPath query.
public:
virtual System::Xml::XPath::XPathNavigator ^ SelectSingleNode(System::String ^ xpath);
public virtual System.Xml.XPath.XPathNavigator? SelectSingleNode (string xpath);
public virtual System.Xml.XPath.XPathNavigator SelectSingleNode (string xpath);
abstract member SelectSingleNode : string -> System.Xml.XPath.XPathNavigator
override this.SelectSingleNode : string -> System.Xml.XPath.XPathNavigator
Public Overridable Function SelectSingleNode (xpath As String) As XPathNavigator
参数
返回
XPathNavigator 对象,其中包含指定的 XPath 查询的第一个匹配节点;如果没有查询结果,则为 null。An XPathNavigator object that contains the first matching node for the XPath query specified; otherwise, null if there are no query results.
例外
XPath 查询中出错,或者 XPath 表达式的返回类型不是节点。An error was encountered in the XPath query or the return type of the XPath expression is not a node.
XPath 查询无效。The XPath query is not valid.
示例
下面的示例基于指定的 XPath 查询选择中的单个节点 XPathNavigator 。The following example selects a single node from the XPathNavigator based on the XPath query specified.
XPathDocument^ document = gcnew XPathDocument("books.xml");
XPathNavigator^ navigator = document->CreateNavigator();
XPathNavigator^ node = navigator->SelectSingleNode("//title");
Console::WriteLine(node->InnerXml);
XPathDocument document = new XPathDocument("books.xml");
XPathNavigator navigator = document.CreateNavigator();
XPathNavigator node = navigator.SelectSingleNode("//title");
Console.WriteLine(node.InnerXml);
Dim document As XPathDocument = New XPathDocument("books.xml")
Dim navigator As XPathNavigator = document.CreateNavigator()
Dim node As XPathNavigator = navigator.SelectSingleNode("//title")
Console.WriteLine(node.InnerXml)
该示例使用 books.xml 文件作为输入。The example takes the books.xml file as an input.
<?xml version="1.0" encoding="utf-8" ?>
<bookstore>
<book genre="autobiography" publicationdate="1981-03-22" ISBN="1-861003-11-0">
<title>The Autobiography of Benjamin Franklin</title>
<author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
<price>8.99</price>
</book>
<book genre="novel" publicationdate="1967-11-17" ISBN="0-201-63361-2">
<title>The Confidence Man</title>
<author>
<first-name>Herman</first-name>
<last-name>Melville</last-name>
</author>
<price>11.99</price>
</book>
<book genre="philosophy" publicationdate="1991-02-15" ISBN="1-861001-57-6">
<title>The Gorgias</title>
<author>
<name>Plato</name>
</author>
<price>9.99</price>
</book>
</bookstore>
注解
SelectSingleNode方法选择与对象中的 XPath 查询匹配的第一个节点 XPathNavigator 。The SelectSingleNode method selects the first node that matches the XPath query in the XPathNavigator object.
适用于
SelectSingleNode(XPathExpression)
使用指定的 XPathNavigator 对象在 XPathExpression 中选择单个节点。Selects a single node in the XPathNavigator using the specified XPathExpression object.
public:
virtual System::Xml::XPath::XPathNavigator ^ SelectSingleNode(System::Xml::XPath::XPathExpression ^ expression);
public virtual System.Xml.XPath.XPathNavigator? SelectSingleNode (System.Xml.XPath.XPathExpression expression);
public virtual System.Xml.XPath.XPathNavigator SelectSingleNode (System.Xml.XPath.XPathExpression expression);
abstract member SelectSingleNode : System.Xml.XPath.XPathExpression -> System.Xml.XPath.XPathNavigator
override this.SelectSingleNode : System.Xml.XPath.XPathExpression -> System.Xml.XPath.XPathNavigator
Public Overridable Function SelectSingleNode (expression As XPathExpression) As XPathNavigator
参数
- expression
- XPathExpression
包含已编译的 XPath 查询的 XPathExpression 对象。An XPathExpression object containing the compiled XPath query.
返回
XPathNavigator 对象,其中包含指定的 XPath 查询的第一个匹配节点;如果没有查询结果,则为 null。An XPathNavigator object that contains the first matching node for the XPath query specified; otherwise null if there are no query results.
例外
XPath 查询中出错,或者 XPath 表达式的返回类型不是节点。An error was encountered in the XPath query or the return type of the XPath expression is not a node.
XPath 查询无效。The XPath query is not valid.
示例
下面的示例 XPathNavigator 基于中包含的 XPath 查询从选择中的单个节点 XPathExpression 。The following example selects a single node from the XPathNavigator based on the XPath query contained in the XPathExpression.
XPathDocument^ document = gcnew XPathDocument("books.xml");
XPathNavigator^ navigator = document->CreateNavigator();
XPathExpression^ query = navigator->Compile("//title");
XPathNavigator^ node = navigator->SelectSingleNode(query);
Console::WriteLine(node->InnerXml);
XPathDocument document = new XPathDocument("books.xml");
XPathNavigator navigator = document.CreateNavigator();
XPathExpression query = navigator.Compile("//title");
XPathNavigator node = navigator.SelectSingleNode(query);
Console.WriteLine(node.InnerXml);
Dim document As XPathDocument = New XPathDocument("books.xml")
Dim navigator As XPathNavigator = document.CreateNavigator()
Dim query As XPathExpression = navigator.Compile("//title")
Dim node As XPathNavigator = navigator.SelectSingleNode(query)
Console.WriteLine(node.InnerXml)
该示例使用 books.xml 文件作为输入。The example takes the books.xml file as an input.
<?xml version="1.0" encoding="utf-8" ?>
<bookstore>
<book genre="autobiography" publicationdate="1981-03-22" ISBN="1-861003-11-0">
<title>The Autobiography of Benjamin Franklin</title>
<author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
<price>8.99</price>
</book>
<book genre="novel" publicationdate="1967-11-17" ISBN="0-201-63361-2">
<title>The Confidence Man</title>
<author>
<first-name>Herman</first-name>
<last-name>Melville</last-name>
</author>
<price>11.99</price>
</book>
<book genre="philosophy" publicationdate="1991-02-15" ISBN="1-861001-57-6">
<title>The Gorgias</title>
<author>
<name>Plato</name>
</author>
<price>9.99</price>
</book>
</bookstore>
注解
SelectSingleNode方法选择与中包含在中的已编译 XPath 查询匹配的第一个节点 XPathExpression XPathNavigator 。The SelectSingleNode method selects the first node that matches the compiled XPath query contained in the XPathExpression object in the XPathNavigator.