SelectSingleNode

CompositeNode.SelectSingleNode method

选择第一个Node与 XPath 表达式匹配。

public Node SelectSingleNode(string xpath)
范围类型描述
xpathStringXPath 表达式。

返回值

第一个Node与 XPath 查询匹配或无效的如果没有找到匹配的节点。

评论

目前仅支持带有元素名称的表达式。不支持使用属性名称的 Expressions 。

例子

展示如何使用 XPath 表达式选择某些节点。

Document doc = new Document(MyDir + "Tables.docx");

// 此表达式将提取所有段落节点,
// 它们是文档中任何表格节点的后代。
NodeList nodeList = doc.SelectNodes("//表格//段落");

// 使用枚举器遍历列表并打印表格中每个单元格中每个段落的内容。
int index = 0;

using (IEnumerator<Node> e = nodeList.GetEnumerator())
    while (e.MoveNext())
        Console.WriteLine($"Table paragraph index {index++}, contents: \"{e.Current.GetText().Trim()}\"");

// 此表达式将选择文档中任何 Body 节点的直接子段落。
nodeList = doc.SelectNodes("//正文/段落”);

// 我们可以将列表视为一个数组。
Assert.AreEqual(4, nodeList.ToArray().Length);

// 使用 SelectSingleNode 选择与上述相同表达式的第一个结果。
Node node = doc.SelectSingleNode("//正文/段落”);

Assert.AreEqual(typeof(Paragraph), node.GetType());

也可以看看