I’m using HtmlAgilityPack and I’d like to select the value from inside the td with class title. I need the value Battlefield 3 which is inside the <a> tag.
I’ve tried the following, just to get the correct td element, and I get an exception object is not set to an instance.
var title = from item in document.DocumentNode.Descendants()
where item.Name == "td" && item.Attributes["class"].Value == "title"
select item.InnerHtml;
With this small example, I’ll probably figure out the rest of what I need.
Thank you for the suggestions.
<tr class="first-in-year">
<td class="year">2011</td>
<td class="img"><a href="/battlefield-3/61-27006/"><img src=
"http://media.giantbomb.com/uploads/6/63038/1700748-bf3_thumb.jpg" alt=""></a></td>
<td class="title">
<a href="/battlefield-3/61-27006/">Battlefield 3</a>
<p class="deck">Battlefield 3 is DICE's next installment in the franchise and
will be on PC, PS3 and Xbox 360. The game will feature jets, prone, a
single-player and co-op campaign, and 64-player multiplayer (on PC). It's due out
in Fall of 2011.</p>
</td>
</tr>
Try using XPath selectors with class or other attribute specifiers.
It should give you all elements within elements that have the class ‘title’. You would then iterate through that NodeCollection and call the node.InnerText property.
The tutorial/resource at W3Schools is pretty good for quick ramp up.