Using XpathNavigator and HtmlAgilityPack, find the InnerHtml value.

c# html-agility-pack xpathnavigator

Question

some of the test.xml

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

/create XpathNavigator to retrieve desc9 as the final value from the td.

> HtmlDocument document = new HtmlDocument();
        document.Load(Server.MapPath("test.xml"));

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

The debugger indicates that the value may be evaluated in the following way, which is highly laborious.

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

How do I get to desc9 the best?

1
1
8/22/2011 2:23:51 PM

Accepted Answer

Although I haven't utilized the XPathNavigator, this alternative uses the HTML Agility Pack and the SelectNodes/SelectSingleNode style.

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

OR

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

FYI - It seems that the examples provided with the Source are the greatest source of documentation for the HTML Agility pack. Why there isn't a separate download for it in the documentation is beyond me.

2
8/22/2011 3:51:28 PM

Popular Answer

I believe your approach to this is flawed.

All you should have to do, in my opinion, is anything along these lines:

document.DocumentNode.SelectSingleNode("/table[1]/tbody[1]/tr[2]/td[9]");

There isn't a copy of the documents online that I can link you to, but you can locate them at http://htmlagilitypack.codeplex.com/releases/view/44954 if you want further information.

Is there another reason you are using the HTML Agility Pack if all you are doing is reading XML, or is your test file the only one that is valid XML?



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