Vb.net 如何在输入元素之前获取标签的内部文本?

Vb.net 如何在输入元素之前获取标签的内部文本?,vb.net,html-agility-pack,Vb.net,Html Agility Pack,我的应用程序正在使用htmlagility pack。现在我可以在一个表单上获取所有的输入元素。问题是我要按ID获取所有输入元素。我试图缩小范围,只给我按ID表单的输入元素,这些元素在每个输入元素之前包含精确的内部文本标签 例如: <label for="email">Email Address:</label> <input type="text" class="textbox" name="email" id="email" maxlength="50" val

我的应用程序正在使用htmlagility pack。现在我可以在一个表单上获取所有的输入元素。问题是我要按ID获取所有输入元素。我试图缩小范围,只给我按ID表单的输入元素,这些元素在每个输入元素之前包含精确的内部文本标签

例如:

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

谢谢大家……我不得不说我已经学习VB.NET一段时间了,到目前为止,这个论坛非常棒……很高兴我找到了它。

这里的基本概念是获取标签,其
for
属性与相关
输入的id匹配

因此,我们首先循环浏览标签,并将标签文本记录在字典中,该字典由
值键入,然后循环浏览
输入
,如果输入的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()

哇!我的一年结束了…再次感谢competent_tech。一旦我有足够的问题向代表提出…我会回来为你介绍一位合适的代表,你应该为我提供这么多帮助。这是个好消息!我还发布了对您问题的回答:
    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()