0

I've been trying to get the value of below using HtmlAgilityPack but without success. I'm using SelectSingleNode.

Here is my html.

<html>
<body>
    <table>
        <tr><td>One</td></tr>       
    </table>
</body>
</html>

And below is the codebehind.

HtmlDocument doc = new HtmlDocument();
doc.Load("C:\\test.html");

HtmlNode test = doc.DocumentNode.SelectSingleNode("/html/body/table/tbody/tr/td");
Console.WriteLine(test.InnerHtml);

The test (HtmlNode) variable is null.

How am I going to get the value inside which is One.

Philip Pittle
  • 11,821
  • 8
  • 59
  • 123
Rod Talingting
  • 943
  • 1
  • 15
  • 25

2 Answers2

1

From Using XPath in SelectSingleNode: Retrieving individual element from XML if it's present you need to use text():

var test = doc.DocumentNode.SelectSingleNode("/html/body/table/tr/td/text()");

Also, you need to remove tbody. There is no tbody in your html.

Community
  • 1
  • 1
Philip Pittle
  • 11,821
  • 8
  • 59
  • 123
  • Why do you expect child node (text) to exist when parent could not be found? – Alexei Levenkov Nov 05 '14 at 17:10
  • Good point. Just noticed the original xpath is reference a `tbody` node that doesn't exist. – Philip Pittle Nov 05 '14 at 17:13
  • +1: for actual answer (`tbody` not present in sample). Note that `text()` is absolutely not related to the issue and not needed (unless actually looking for text nodes inside "td", which is not what OP's sample shows - InnerHtml includes all nodes too - also the same for particular sample) – Alexei Levenkov Nov 05 '14 at 17:23
  • Thanks also for replying to my question. I replied to Alexei's answer above the reason why there is tbody in my xpath. I know that there is no tbody but when I used Chrome's developer tool (F12) then copy the xpath of the elemen (right click), the xpath includes the tbody text. – Rod Talingting Nov 05 '14 at 18:22
1

As shown your sample does not have tbody node so your XPath would return no nodes (null for SelectSingleNode).

Fix:

HtmlNode test = doc.DocumentNode.SelectSingleNode("/html/body/table/tr/td");

Debugging tip: build XPath slowly and check each result/child nodes:

     "/html/body/table/" - some children
     "/html/body/table/tbody/"  - null
     "/html/body/table/tr/" - some children
     "/html/body/table/tr/td" - score
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • Thanks for this. Yes, I know that there is no tbody but when I used Chrome's developer tool (F12) then copy the xpath of the elemen (right click), the xpath includes the tbody text. – Rod Talingting Nov 05 '14 at 18:19