Janno
Janno

Reputation: 149

HtmlAgilityPack - Get only the first match

Im new to HtmlAgilityPack and i cant figure out how to stop the search after the first match has been found.

Dim site As HtmlAgilityPack.HtmlWeb = New HtmlWeb()
Dim document As HtmlAgilityPack.HtmlDocument = site.Load("website")

For Each table As HtmlNode In document.DocumentNode.SelectNodes("//td[@class='forum_thread_post']//a[@href]")
        ListBox1.Items.Add(table.InnerText)
Next

The problem is that the website contains many td[@class='forum_thread_post' nodes and i only need the first one. I experimented with SelectSingleNode too, but i couldnt even get that to work but im thinking that is the way to do it? If getting the single match to a textbox is better/easier i would want that.

Here is a picture: http://oi42.tinypic.com/25fmwr5.jpg I want to get the a title or a alt from the picture

Upvotes: 1

Views: 1227

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460380

What was wrong with SelectSingleNode?

Dim table = document.DocumentNode.SelectSingleNode("//td[@class='forum_thread_post']//a[@href]")
ListBox1.Items.Add(table.InnerText)

If the InnerText could be empty:

Dim tables = document.DocumentNode.SelectNodes("//td[@class='forum_thread_post']//a[@href]")
Dim firstNotEmpty = tables.FirstOrDefault(Function(t) Not String.IsNullOrWhiteSpace(t.InnerText))

Upvotes: 1

Related Questions