这是一个更大的HTML文件中的示例HTML输入部分。
string html = "<html> <p>Ingredients:</p> </html>";
我想要获取仅包含Ingredients文本的节点。 Ingredients可能出现在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