2011-09-22 24 views
1

我試圖從遠程頁面抓取html表格,並在我的網站上的htmltable中顯示此表格的內容。我正在使用htmlagility包。到目前爲止,這裏是我的代碼:從html中提取表格轉換爲html.net中的htmltable vb(htmlagilitypack)

Imports HtmlAgilityPack 
Partial Class ContentGrabExperiment 
    Inherits System.Web.UI.Page 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
     'fetch the remote html page 
     Dim web As New HtmlWeb() 
     Dim html As HtmlAgilityPack.HtmlDocument = web.Load("http://www.thesite.com/page.html") 

     'Create table 
     Dim outputTable As New HtmlTable 
     Dim tableRow As New HtmlTableRow 
     Dim tableCell As New HtmlTableCell 


     'Target the <table> tag 
     For Each table As HtmlNode In html.DocumentNode.SelectNodes("//table") 
      'Target the <tr> tags within the table 
      For Each row As HtmlNode In table.SelectNodes("//tr") 
       'Target the <td> tags within the <tr> tags 
       For Each cell As HtmlNode In row.SelectNodes("//td") 
        'Set the value to that of the <td> 
        tableCell.InnerText = cell.InnerHtml 
        'Add the cell to the row 
        tableRow.Cells.Add(tableCell) 
       Next 
       'Add row to the outputTable 
       outputTable.Rows.Add(tableRow) 
      Next 
     Next 
     'Add the table to the page 
     PlaceHolderTable.Controls.Add(outputTable) 
    End Sub 
End Class 

從此我期待得到充分的表從頁面的innerText,作爲HTMLTABLE,我可以再操作。我得到這個代碼是:

<table> 
    <tr> 
     <td>&amp;nbsp;</td> 
    </tr> 
</table> 

請有人指出我的錯誤與我的語法。任何幫助非常感謝!

回答

1

1)您只有一個TableRow和一個TableCell。您需要爲每個行/單元創建一個新的。你可以重新使用這些變量,但是你需要在它們中新增一個對象。

2)您可能需要選擇./tr./td以獲取當前表/行中的行和單元格。