HtmlAgilityPack if div exists

c# html-agility-pack

Question

I am using Html Agility Pack to see if divs with a specific class and id exist.

 string target = "http://192.168.3.230/index.htm";
 WebClient client = new WebClient();
 string html = client.DownloadString(target);
 HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
 doc.LoadHtml(html);

 if (doc.DocumentNode.SelectSingleNode("//div[@id='bit0' and @class='rledoff']") != null){
      //actions in here
 }

 else if (doc.DocumentNode.SelectSingleNode("//div[@id='bit0' and @class='rledon']") != null)
 {
      //actions in here
 }

Currently the else if should be true, but it is doing the actions in the if statement. I have seen this work correctly when I did

doc.Load("c:\\somelocaldest\\page.htm");

When I try to do it from the actual site instead of a locally saved file of the site it will see that the id is correct and disregard the class. What could cause it to behave differently between getting the html for a local file and from an external site/device?

Popular Answer

First of all I suggest you to do this, for me it worked faster than WebClient.

HtmlWeb web = new HtmlWeb();
HtmlDocument doc = web.Load(target);

If you say that this works on local hmtl file and not work when the html is on the server, then do the following.

File.WriteAllText(path,doc.DocumentNode.OuterHtml);

Sometimes the source code of page its differ when you download it with htmlweb (or webclient) so make again your xpath by using this new html file.




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