我正在使用HTML敏捷包从节点中提取文本。
var sb = new StringBuilder();
foreach (HtmlNode innernode in node.SelectNodes("//*[not(self::script or self::style)]/text()[not(normalize-space(.)='')]"))
{
sb.Append(innernode.InnerText);
}
Console.WriteLine(sb.ToString());
我正在使用此代码。我想从“节点”及其子节点中提取文本,但是这个xpath查询返回整个html文档的结果(它开始从根节点开始搜索)。我知道这是愚蠢的,但是如何更新XPath以便它只在“node”的子节点中搜索:)
由于
答案 0 :(得分:2)
要包含来自node
的子节点的文本节点(我还假设所有其他后代),以及node
的文本节点,您可能需要:
./descendant-or-self::*[not(self::script or self::style)]/text()[not(normalize-space(.)='')]
.//*[not(self::script or self::style)]/text()[not(normalize-space(.)='')]
不会包含node
的直接子节点文本节点,因为它意味着./descendant-or-self::*/*[not(self::script or self::style)]...
。