我想访问第二个TR的第二个TD的c#,innertext,即john paul和30个html敏捷包?
<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"> </td>
</tr>
<tr class="trRow">
<td width="190" class="tdDataRow">name:</td>
<td class="tdDataName">john paul</td>
<td class="whiteClass"> </td>
</tr>
<tr class="trRow">
<td width="190" class="tdDataRow">age:</td>
<td class="tdDataName">30</td>
<td class="whiteClass"> </td>
</tr>
答案 0 :(得分:5)
加载HtmlDocument
后(new HtmlWeb().Load("http://www.site.com")
或doc.Load(...)
),您可以执行以下操作:
//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);
}
修改强>
我编辑了代码,因为<tr>
标记不在<form>
内。
答案 1 :(得分:0)
将属性runat="server"
添加到您要访问的td
<td id="tdname" runat="server" class="tdDataName">john paul</td>
然后你可以直接访问内部文本
string name=tdname.InnerText; //where tdname is the id of td