Search code examples
c#asp.net-mvchtml-agility-pack

Select specific td using htmlagilitypack


I'm trying to select a specific td Value from this table:

<div id="mangaproperties">
<table><tbody>
<tr>
<td>Name:</td>
<td><h2> Ichiba Kurogane</h2></td>
</tr>
<tr>
<td>Alternate Name:</td>
<td>Wants to Earn Income</td>
</tr></tbody></table></div>

Now what i want to get is this td value:

<td>Wants to EarnIncome</td>

I have tried this code:

var div3 = document1.DocumentNode.SelectNodes("//*[@id='mangaproperties']/table/*/tr[1]/td[1]");

But iit's not worked for me .. Can someone let me know how i can get the specified td value ?


Solution

  • You can try to use tr[2]/td[2] instead of tr[1]/td[1], because the paser started with 1 instead of 0

    var div3 = document1.DocumentNode.SelectNodes("//*[@id='mangaproperties']/table/*/tr[2]/td[2]");
    

    Then you can use to get the value.

    div3.FirstOrDefault().InnerText
    

    c# online