XPathNavigator.SelectSingleNode 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
选择 XPathNavigator 中的单个节点。
重载
SelectSingleNode(String, IXmlNamespaceResolver) |
通过指定的 XPathNavigator 对象使用指定的 XPath 查询在 IXmlNamespaceResolver 中选择单个节点,以解析命名空间前缀。 |
SelectSingleNode(String) |
使用指定的 XPath 查询选择 XPathNavigator 中的单个节点。 |
SelectSingleNode(XPathExpression) |
使用指定的 XPathNavigator 对象在 XPathExpression 中选择单个节点。 |
SelectSingleNode(String, IXmlNamespaceResolver)
通过指定的 XPathNavigator 对象使用指定的 XPath 查询在 IXmlNamespaceResolver 中选择单个节点,以解析命名空间前缀。
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 查询中的命名空间前缀。
返回
XPathNavigator 对象,其中包含指定的 XPath 查询的第一个匹配节点;如果没有查询结果,则为 null
。
例外
XPath 查询中出错,或者 XPath 表达式的返回类型不是节点。
XPath 查询无效。
示例
以下示例根据指定的 XPath 查询从 XPathNavigator 对象中选择单个节点,并使用 XmlNamespaceManager 指定的对象解析 XPath 查询中的命名空间前缀。
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
文件作为输入。
<?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 查询匹配的第一个节点。 该方法 SelectSingleNode 使用 IXmlNamespaceResolver 对象解析命名空间前缀。
备注
在 XPath 中,所有命名空间都必须在查询中作为前缀。 如果 XML 文档使用默认命名空间,则仍必须将命名空间前缀添加到 IXmlNamespaceResolver 对象。
适用于
SelectSingleNode(String)
使用指定的 XPath 查询选择 XPathNavigator 中的单个节点。
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
。
例外
XPath 查询中出错,或者 XPath 表达式的返回类型不是节点。
XPath 查询无效。
示例
以下示例根据指定的 XPath 查询从中选择 XPathNavigator 单个节点。
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
文件作为输入。
<?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 选择与对象中的 XPathNavigator XPath 查询匹配的第一个节点。
适用于
SelectSingleNode(XPathExpression)
使用指定的 XPathNavigator 对象在 XPathExpression 中选择单个节点。
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 对象。
返回
XPathNavigator 对象,其中包含指定的 XPath 查询的第一个匹配节点;如果没有查询结果,则为 null
。
例外
XPath 查询中出错,或者 XPath 表达式的返回类型不是节点。
XPath 查询无效。
示例
以下示例从包含XPathExpression的 XPathNavigator XPath 查询中选择单个节点。
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
文件作为输入。
<?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选择与对象XPathNavigator中包含的XPathExpression已编译的 XPath 查询匹配的第一个节点。