【问题标题】:Trying to get inputs / getelementbyID or Class and put into richtextbox试图获取输入/getelementbyID 或 Class 并放入richtextbox
【发布时间】:2011-12-05 02:44:54
【问题描述】:
我目前正在使用 HtmlAgility Pack 为表单输入标签解析一些 HTML,然后获取 ID 或类的名称并列出输入和 id="something here 或 input: class="something here"放入 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
【问题讨论】:
标签:
.net
html-parsing
html-agility-pack
【解决方案1】:
您可以这样做(请注意,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