2012-01-01 92 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

相關問題