HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(content);
var nodes = doc.DocumentNode.SelectNodes("//div[@class=\"noprint res\"]/div");
if (nodes != null)
{
foreach (HtmlNode data in nodes)
{
// Works but not what I want
MessageBox.Show(data.InnerHtml);
// Should work ? but does not ?
MessageBox.Show(data.SelectSingleNode("//span[@class=\"pp-place-title\"]").InnerText);
}
}
I am trying to parse the results of a HTML, the initial node for the foreach, works just as expected and gives me a result of 10 items which matchs what I need.
When I get into the foreach, if I output the inner html of the data item it display the correct data but if I output the SelectSingleNode it will always display the data from the first item from the foreach, is that a normal behavior or am I doing something wrong ?
In order to resolve the issue I had to create a new html inside the foreach for every data item like this:
HtmlAgilityPack.HtmlDocument innerDoc = new HtmlAgilityPack.HtmlDocument();
innerDoc.LoadHtml(data.InnerHtml);
// Select what I need
MessageBox.Show(innerDoc.DocumentNode.SelectSingleNode("//span[@class=\"pp-place-title\"]").InnerText);
Then I get the correct per item data.
The page I was trying to get data from was http://maps.google.com/maps?q=consulting+loc:+US if u want to try and see what happens for yourself.
Basically I am reading the left side column for company names and the above happens.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With