2012-01-01 6 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に感謝します。私はrepを与えるために構築された十分な質問を得るとすぐ....私はあなたが私をそんなに手助けするために値する適切な担当者とあなたをフックするために戻ってきます。 –

+0

それは素晴らしいニュースです!あなたの質問への回答も掲載しました:http://stackoverflow.com/questions/8380486/html-agility-pack-trying-to-get-inputs-getelementbyid-or-class-and-put-into –

関連する問題