Using XpathNavigator and HtmlAgilityPack, find the InnerHtml value.

c# html-agility-pack xpathnavigator


portion of test.xml

<tr class="a"> 
    <td align="left" nowrap="true">desc1</td> 
    <td align="left">desc2</td>  
    <td align="left">desc4</td> 
    <td align="left">desc5</td>
    <td class="nr">desc9</td>

//create XpathNavigator to get the last value inside td i.e. desc9

> HtmlDocument document = new HtmlDocument();

        XPathNavigator xPathNavigator = document.CreateNavigator();
        object o = xPathNavigator.Evaluate("/table[1]/tbody[1]/tr[2]/td[9]");

The debugger shows the value can be evaluated as below which is very cumbersome.

((HtmlAgilityPack.HtmlNodeNavigator)((new System.Linq.SystemCore_EnumerableDebugView(((MS.Internal.Xml.XPath.XPathSelectionIterator)(o)))).Items[0])).Value

What is the best way to get to desc9?

8/22/2011 2:23:51 PM

Accepted Answer

I haven't used the XPathNavigator but here is a similar solution with the SelectNodes/SelectSingleNode style and the HTML Agility Pack.

string xPathSearch = "/table[1]/tbody[1]/tr[2]";
HtmlNode tableRow = doc.DocumentNode.SelectSingleNode(xPathSearch);
string description9 = tableRow.ChildNodes[9].InnerText;


string xPathSearch = "/table[1]/tbody[1]/tr[2]/td[9]";
HtmlNode tableColumn = doc.DocumentNode.SelectSingleNode(xPathSearch);
string description9 = tableColumn.InnerText;

FYI - The best documentation on the HTML Agility pack seems to be the samples included with the Source. Not sure why that isn't a separate download in the documentation.

8/22/2011 3:51:28 PM

Popular Answer

I think you are going about this wrong.

I believe all you should need to do is something along the lines of:


I can't find an online copy of the docs to link you to but you can check the docs found at for more details.

Also if you are just reading XML is there any reason why you are using the html agility pack or is it just your test file that is valid XML?

Related Questions


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