2013-08-28 51 views
0

什麼即時試圖做的是讓數據從該表中:HTML agilitypack取出數據表的

<div class="fliptable"> 
<div class="head"> 
    <div class="outer"> 
     <div class="inner"> 
      Information</div> 
    </div> 
</div> 
<table class="body"> 
    <tbody> 
     <tr> 
      <td class="key" style=""> 
       SKU 
      </td> 
      <td class="right"> 
       SKU1<br> 
       SKU2<br> 
       SKU3<br> 
      </td> 
     </tr> 
     <tr> 
      <td class="key" style=""> 
       EAN 
      </td> 
      <td class="right" style=""> 
       EAN1<br> 
       EAN2<br> 
       EAN3<br> 
      </td> 
     </tr> 
    </tbody> 
</table> 

代碼我到目前爲止:

Dim doc As New HtmlAgilityPack.HtmlDocument() 
    doc.Load(WebBrowser1.DocumentStream) 

    Try 

     Dim key = doc.DocumentNode.SelectSingleNode("//div[@class='fliptable']//table[@class='body']//tr//td[@class='key']") 
     RichTextBox1.AppendText(key.InnerText & vbNewLine) 

     Dim right As HtmlNode = doc.DocumentNode.SelectSingleNode("//div[@class='fliptable']//table[@class='body']//tr//td[@class='right']") 
     For Each p As HtmlNode In right.SelectNodes("//div[@class='fliptable']//table[@class='body']//tr//td[@class='right']//br") 
      RichTextBox1.AppendText(p.PreviousSibling.InnerText.Trim() & vbNewLine) 
     Next 


    Catch ex As Exception 

    End Try 

我得到的結果是:

SKU 
SKU1 
SKU2 
SKU3 
EAN1 
EAN2 
EAN3 

我會像:

SKU 
SKU1 
SKU2 
SKU3 
EAN 
EAN1 
EAN2 
EAN3 

,但我不能爲得到我的人生中,工作

回答

0

這工作:

Dim bodyTable = (From tbl In doc.DocumentNode.Descendants("table") 
       Where tbl.Attributes.Contains("class") AndAlso tbl.Attributes("class").Value = "body").FirstOrDefault 
If bodyTable IsNot Nothing Then 
    Dim allTd = bodyTable.SelectNodes("//td") 
    For Each td In allTd 
     For Each token In td.InnerText.Split(New String() {}, StringSplitOptions.RemoveEmptyEntries) 
      RichTextBox1.AppendText(token.Trim & Environment.NewLine) 
     Next 
    Next 
End If