HtmlAgilityPack多种类型的后代

时间:2012-12-17 18:10:29

标签: html linq html-agility-pack

我试图选择div,span,labels等基本上任何具有特定属性的元素。

IEnumerable<HtmlNode> allDivsWithItemType = _doc.DocumentNode.Descendants("div").Where(d => d.Attributes.Contains("itemtype"));

有没有办法将所有后代绳索成上面的一个?因为上面只明显找到了div。我试图避免重复的代码添加一个额外的行来替换一个单词。

例如(不起作用)

IEnumerable<HtmlNode> allDivsWithItemType = _doc.DocumentNode.Descendants("*").Where(d => d.Attributes.Contains("itemtype"));

1 个答案:

答案 0 :(得分:4)

尝试:

IEnumerable<HtmlNode> allDivsWithItemType = _doc.DocumentNode.Descendants()
   .Where(d => d.Attributes.Contains("itemtype"));
相关问题