2012-01-01 52 views
1

我的应用程序使用htmlagility包。截至目前,我可以获取表单上的所有输入元素。问题是我通过ID获取所有输入元素。我试图缩小它只给我一个表单的输入元素,其中包含ID在每个输入元素之前包含确切的内部文本标签。如何在输入元素之前获取标签的内部文本?

例子:

<label for="email">Email Address:</label> 
<input type="text" class="textbox" name="email" id="email" maxlength="50" value="" dir="ltr" tabindex="1" 

我想获得具有程序标签与“电子邮件地址”

内文字如何将我这个字输入?

这是我的应用程序,通过ID获取所有输入元素。

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 

    Dim doc As HtmlDocument 
    Dim web As New HtmlWeb 
    doc = web.Load("http://shaggybevo.com/board/register.php") 
    Dim docNode As HtmlNode = doc.DocumentNode 
    Dim nodes As HtmlNodeCollection = docNode.SelectNodes("//input") 
    'SelectNodes takes a XPath expression 
    For Each node As HtmlNode In nodes 
     'Get all input elements by id 
     Dim id As String = node.GetAttributeValue("value", "id") 

     'print all input elements by id to form2 richtextbox 
     Form2.RichTextBox1.Text = Form2.RichTextBox1.Text & Environment.NewLine & id.ToString & name.ToString() 
     Form2.Show() 

    Next 

End Sub 

谢谢你们....我必须说,我一直在学习VB.NET了一会儿,迄今为止这个论坛已经真棒......高兴,我发现它..

回答

0

的这里的基本概念是获取其for属性与关联的input的ID匹配的标签。

因此,我们将循环标签第一,并通过inputs记录标签的文本在由for值键控字典,那么我们就循环,如果输入的ID在字典中,我们检索值从字典(这是标签文本)并显示它。

请注意,我还修改了如何收集数据以提高效率(几乎每次连接字符串时都应该使用stringbuilder)。

这里是重写代码:

Dim web As HtmlAgilityPack.HtmlWeb = New HtmlWeb() 
    Dim doc As HtmlAgilityPack.HtmlDocument = web.Load("http://shaggybevo.com/board/register.php") 
    Dim nodes As HtmlNodeCollection 

    ' Keeps track of the labels by the associated control id 
    Dim labelText As New System.Collections.Generic.Dictionary(Of String, String) 

    ' First, get the labels 
    nodes = doc.DocumentNode.SelectNodes("//label") 

    If nodes IsNot Nothing Then 
     For Each node In nodes 
      If node.Attributes.Contains("for") Then 
       Dim sFor As String 

       ' Extract the for value 
       sFor = node.Attributes("for").Value 

       ' If it does not exist in our dictionary, add it 
       If Not labelText.ContainsKey(sFor) Then 
        labelText.Add(sFor, node.InnerText) 
       End If 
      End If 
     Next 
    End If 

    nodes = doc.DocumentNode.SelectNodes("//input") 

    Dim sbText As New System.Text.StringBuilder(500) 

    If nodes IsNot Nothing Then 
     For Each node In nodes 
      ' See if this input is associated with a label 
      If labelText.ContainsKey(node.Id) Then 
       ' If it is, add it to our collected information 
       sbText.Append("Label = ").Append(labelText(node.Id)) 
       sbText.Append(", Id = ").Append(node.Id) 

       sbText.AppendLine() 
      End If 
     Next 
    End If 

    Form2.RichTextBox1.Text = sbText.ToString 
    Form2.Show() 
+0

WOW!我的一年是....再次感谢competent_tech。只要我得到足够的问题来给代表....我会回来钩你与适当的代表,你应该帮助我这么多。 – 2012-01-01 19:29:33

+0

好消息!我也发布了一个答案你的问题:http://stackoverflow.com/questions/8380486/html-agility-pack-trying-to-get-inputs-getelementbyid-or-class-andputputin – 2012-01-01 19:45:55

相关问题