2011-05-09 67 views
3

我想在c#中訪問第二個TR的第二個TD,即john paul和帶有html敏捷性包的30個innertextxt?使用html敏捷包訪問第二TR的第二個TD的內文?

<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> 
+0

看看代碼,雖然這將是很好的粘貼TD整個HTML代碼,或者至少關閉'

'和''標籤。 '
'應該在''裏面...... – 2011-05-09 19:40:56

回答

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>

0

附加屬性runat="server"您要訪問

 <td id="tdname" runat="server" class="tdDataName">john paul</td> 

然後ü可以直接訪問內部文本作爲

string name=tdname.InnerText;  //where tdname is the id of td