HtmlAgilityPack method for returning value from a row in a table given column data

c# html-agility-pack

Question

I want to utilize HTML Agility Pack to extract inner HTML information depending on how close it is to other tds in a certain table row.

What would be an effective dynamic command in the code below, for instance, that would accept the data in the first td tag as an input and output the data in the third td tag?

<tbody>
    <tr>
        <td>Smith</td>
        <td>Glasnevin</td>
        <td>11</td>
        <td>6.0</td>
    </tr>
    <tr>
        <td>Harty</td>
        <td>Blackrock</td>
        <td>11</td>
        <td>6.0</td>
    </tr>
    <tr>
        <td>Power</td>
        <td>Salthill</td>
        <td>0</td>
        <td>5.8</td>
    </tr>
</tbody>

Therefore, if I enter "Smith," I want to obtain "11" as an output, and if I input "Power," I want to get "0."

1
0
9/9/2014 12:46:13 AM

Accepted Answer

Here's an illustration:

HtmlDocument doc = new HtmlDocument();
//load your HTML to `doc`
var result = doc.DocumentNode
                .SelectSingleNode("//tr[td[1]='Power']/td[3]")
                .InnerText;
//at this point `result` hold value of 0

XPath stands for select.<tr> node with a newborn node<td> equals "Power" then get the third<td> from the previously stated<tr> .

1
9/9/2014 12:46:02 AM


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