2011-12-05 27 views
0

我目前使用HtmlAgility包首先解析一些表單輸入標記的HTML,然後獲取ID或類的名稱,並列出輸入和id = 「這裏的東西或輸入:類=」這裏的東西」變成一個RichTextBox審查試圖獲取輸入/ getelementbyID或類,並將其放入richtextbox

這裏是我的代碼

Dim web As HtmlAgilityPack.HtmlWeb = New HtmlWeb() 
Dim doc As HtmlAgilityPack.HtmlDocument = web.Load(TextBox1.Text) 
Dim threadLinks As IEnumerable(Of HtmlNode) = doc.DocumentNode.SelectNodes("/input") 

For Each link In threadLinks 
Dim str As String = link.InnerHtml 
RichTextBox1.Text = str.ToString 

Next link 

End Sub 
+0

試過link.OuterHtml? – sisve

回答

0

這裏是你如何能做到這一點(注意的SelectNodes選擇字符串是固定的。 ):

Dim threadLinks As IEnumerable(Of HtmlNode) = doc.DocumentNode.SelectNodes("//input") 

    ' Use a stringbuilder to hold all of the retrieved information 
    Dim sbText As New System.Text.StringBuilder(5000) 

    If threadLinks IsNot Nothing Then 
     For Each link In threadLinks 
      ' Add information about each found input on a new line 
      sbText.Append("Id = ").Append(link.Id) 

      ' The class is held in an attribute, so ensure the attribute exists before using it 
      If link.Attributes.Contains("Class") Then 
       ' Add the value of the class attribute to the output 
       sbText.Append(", Class = ").Append(link.Attributes("Class").Value) 
      End If 

      ' Separate this item from the next by adding a new line 
      sbText.AppendLine() 
     Next 
    End If 

    ' Finally, send the retrieved information to the textbox. 
    RichTextBox1.Text = sbText.ToString 
相關問題