HtmlAgilityPack Multiple Types of Descendants

html html-agility-pack linq

Question

I am trying to select a divs, spans, labels, etc basically any element with a certain attribute.

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

Is there a way to rope all descendants into one like above? Since above only finds divs obviously. I am trying to avoid duplicate code to add a whole extra line to replace one word.

For Example (Doesn't work)

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

Accepted Answer

Try:

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



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why