C# HtmlAgilityPack HtmlNodeCollection SelectNodes not working

c# html html-agility-pack selectnodes xpath

Question

I'm using this line of code; however, when I check the watch window, "c" is null.

HtmlNodeCollection c = doc.DocumentNode.SelectNodes("//*[@id=\"content\"]/table/tbody/tr[2]/td/center[2]/b");

But the watch window indicates that it is a legitimate declaration when I declare "c" as this HtmlNodeCollection

HtmlNodeCollection c = new HtmlNodeCollection(doc.DocumentNode.ParentNode);

Then, when I set 'c' to the first piece of code, it reverts to being null.

Since I got the XPath from the Chrome Inspect Element of the element I'm looking for, I know it's accurate.

1
0
6/16/2015 4:27:57 PM

Popular Answer

If no nodes are present, SelectNodes returns null.

Because you created your XPATH using a browser's (such as Chrome, Firefox, etc.) instructions, you believe it to be valid. Unfortunately, this XPATH differs somewhat from the one you obtained through the network (or a file, or a raw stream).

Browsers depend on their own in-memory DOM, which might vary significantly. Because of this, components like TBODY that are exclusive to DOM and not markup may be seen (where they are optional).

Therefore, I advise you to return to the string or stream you provided to the HTML Agility Pack and double-check the XPATH. For a start, I wager that TBODY doesn't exist.

2
6/16/2015 4:35:45 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow