Search code examples
c#htmlhtml-agility-pack

HtmlAgilityPack - How to extract data?


How to get 1988 from the following HTML using HtmlAgilityPack:

<span class="year">Year: <strong class="">1988</strong></span>

I tried with:

.Descendants("span").Where(n => n.GetAttributeValue("class", "").Equals("year")).First().Attributes["strong"].DeEntitizeValue;

but null value is returned.


Solution

  • You want InnerText of the strong element. In your example you could do just

    SelectSingleNode("//strong").InnerText
    

    Modify the strong selector appropriatelly for you use case.

    Documentation here