【发布时间】:2016-06-17 06:48:13
【问题描述】:
这是我较大的 html 文件的示例 html 输入部分。
string html = "<html> <p>Ingredients:</p> </html>";
我只想检索具有内部文本成分的节点。 成分可能出现在 html 节点 p、div、strong 等中。
我使用 HtmlAgility 包和 linq 实现此目的的 c# 代码是
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
List<HtmlNode> ingredientList = doc.DocumentNode.Descendants().Where
(x => x.InnerText.Contains("Ingredients:")).ToList();
这段代码的结果给了我 3 个节点
<html> node
<p> node
#text node
我只想检索
<p> node
【问题讨论】: