"Expression must evaluate to a node-set" 尝试 select 一个 HTML 节点时

"Expression must evaluate to a node-set" when trying to select a HTML node

我正在尝试使用 HtmlAgilityPack 获取 HTML 文件的特定部分。

输入HTML文件(经过简化和清理):

<html>
<head>
</head>
<body>
    <section>
        <div>
        </div>
    </section>
    <section>
        <div>
        </div>
        Line 1
        Line 2 - Text here
        Line 3
        <div>
        </div>
    </section>
</body>
</html>

这是我的代码:

Dim getPage As HtmlDocument = webGet.Load(Uri)
Dim AllTextLines As HtmlNodeCollection = getPage.DocumentNode.SelectNodes("/html/body/section[2]/text()")
Dim SecondTextLine As HtmlNodeCollection = getPage.DocumentNode.SelectNodes("/html[1]/body[1]/section[2]/#text[2]")

设置 SecondTextLine 的值失败并出现此错误:

System.Xml.XPath.XPathException: Expression must evaluate to a node-set

我想得到的是:

  1. 仅包含 "Line 2 - Text here"
  2. 的行
  3. 一节中的所有文本都没有子元素(<div> 在我的例子中)但作为一个节点,一个包含所有文本的字符串。

我该怎么做?

编辑: 我再次添加了 XPath 标记,因为返回的错误直接来自 System.Xml.XPath.XPathException,而不是来自 HtmlAgilityPack。

字符串 /html[1]/body[1]/section[2]/#text[2] 不是有效的 XPath 表达式。糟糕的错误消息,但您肯定不会期望它 select 任何东西。

如果您想要第二个文本节点,那就是 /html[1]/body[1]/section[2]/text()[2]。但是如果你想要文本 "Line 2 - text here" 那么你不是 select 节点,你是 select 节点中的字符串,所以 SelectNodes() 是错误的方法正在打电话。您似乎正在使用 XPath 1.0 处理器,因此在 XPath 表达式本身内进行字符串操作将非常困难,通常最简单的方法是 return 整个节点到宿主语言并在那里进行字符串操作。或者切换到 XPath 2.0 处理器,然后您可以使用诸如 tokenize() 之类的函数在换行符边界上拆分文本。