Access innertext of 2nd TR's 2nd TD with html agility pack?

c# html html-agility-pack

Question

i want to access in c#, innertext of 2nd TR's 2nd TD, i.e. john paul and 30 with html agility pack?

<table width="100%" border="0" cellpadding="0" cellspacing="0" class="tableClass">
    <form name="form1" action="goto.php" method="post">
        <tr>

            <td height="35" colspan="2" class="tdClass"
                style="padding-top:5px "><img
                src="./include/gif/grey-sub-header-ex-customer.gif"
                alt="details" width="162" height="20" /></td>
            <td valign="top" class="tdClass">&nbsp;</td>
        </tr>

        <tr class="trRow">
            <td width="190" class="tdDataRow">name:</td>
            <td class="tdDataName">john paul</td>
            <td class="whiteClass">&nbsp;</td>
        </tr>

        <tr class="trRow">
            <td width="190" class="tdDataRow">age:</td>
            <td class="tdDataName">30</td>
            <td class="whiteClass">&nbsp;</td>
        </tr>

Popular Answer

Once you have the HtmlDocument loaded (either by new HtmlWeb().Load("http://www.site.com") or by doc.Load(...)) you can do:

//Get 2nds <td> tags inside all tr class of that table
var tds = doc.DocumentNode.SelectNodes("//table[@class='tableClass']/tr[@class='trRow']/td[2]");
foreach (var td in tds) {
    Console.WriteLine(td.InnerText);
}

Edit:
I edited the code because <tr> tags weren't inside <form>.




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why