Htmlagilitypack - 从同一行读取两个不同的节点并将它们添加到列表视图

Htmlagilitypack - reading two different nodes from the same line and add them to listview

我正在尝试使用 (span class=text) 从列表中获取所有参与者名称值以及使用 (class=contenu) 的短名称值并将它们添加到不同的列将显示为类似于网页的列表视图项目。用户可以看到参与者的全名,也可以单击个人参与者品牌(简称)以获取更多详细信息。

Imports HtmlAgilityPack

Public Class Form1

Dim web As New HtmlWeb
Dim doc As HtmlDocument = web.Load("http://www.eurovent-certification.com/en/Certified_products/Access_by_programme.php?rub=04&srub=01&ssrub=&lg=en&select_prog=AHU")
Dim ParticipantNodesShort As HtmlNodeCollection = doc.DocumentNode.SelectNodes("/html/body/table/tr/td[2]/table[4]/tr[2]/td[2]//a[@class='contenu']")
Dim ParticipantNodesLong As HtmlNodeCollection = doc.DocumentNode.SelectNodes("/html/body/table/tr/td[2]/table[4]/tr[2]/td[2]//span[@class='texte']")


Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
    ' Adding ListView Columns
    ListView2.Columns.Add("Participant's contact - Full Name", 250, HorizontalAlignment.Left)
    ListView2.Columns.Add("Brand Name", 100, HorizontalAlignment.Left)
    ListView2.Columns.Add("Brand Name", 100, HorizontalAlignment.Left)
    ListView2.Columns.Add("Brand Name", 100, HorizontalAlignment.Left)
    ListView2.Columns.Add("Brand Name", 100, HorizontalAlignment.Left)

End Sub


Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

          Dim web As New HtmlWeb
    Dim doc As HtmlDocument = web.Load("http://www.eurovent-certification.com/en/Certified_products/Access_by_programme.php?rub=04&srub=01&ssrub=&lg=en&select_prog=AHU")
    Dim ParticipantNodes As HtmlNodeCollection = doc.DocumentNode.SelectNodes("/html/body/table/tr/td[2]/table[4]/tr[2]/td[2]//span[@class='texte'] | /html/body/table/tr/td[2]/table[4]/tr[2]/td[2]//a[@class='contenu']")
    Dim participantname(20) As String
    Dim brandname(50) As String
    Dim participantcount As Integer = 0
    Dim brandcount As Integer = 0

    For Each item As HtmlNode In ParticipantNodes
        If item.Name = "span" Then
            Debug.Print(participantname(participantcount))
            participantname(participantcount) = item.InnerText.Replace(vbLf, "").Replace(vbCr, "").Replace(vbTab, "")
            participantcount = participantcount + 1
        Else
            If Not item.Attributes.Count = 3 Then
                brandname(brandcount) &= String.Format(" [{0}]", item.InnerText.Replace(vbLf, "").Replace(vbCr, "").Replace(vbTab, ""))
                brandcount = brandcount + 1
            End If
        End If
        ListView2.Items.Add(participantname(participantcount) & " " & brandname(brandcount))

    Next
End Sub
End Class

尽量简化问题;

这是整页中显示参与者姓名和品牌的部分(有些有一个,有些有两个、三个或四个)

像下面一样; Mekar srl(参与者姓名)有 4 个不同的品牌,用逗号分隔(Eden、Mekar VENCO、Venticlima)

梅卡s.r.l。 ( EDEN 、 MEKAR 、 VENCO 、 VENTILCLIMA )

但有些只有一个;

像下面的例子; Menegra Gmbh(参与者姓名)只有一个品牌 Menegra MENERGA GmbH(美能佳)

所以我会尝试将这些数据与页面几乎相同的逻辑写入列表视图;

第一列将列出参与者名称,第二列将在同一行列出品牌名称(或者如果超过一个,同一行的第三和第四列)

无论从列表视图如何,我都需要了解保存从网页获取的数据以记录在数组类型字符串中并稍后在列表视图中显示它们。

使用上面的代码,我连续读取所有节点并丢失了它们之间的关系(例如具有多个品牌的参与者名称)

希望这个声明能更加明确,也希望再次得到更多的支持

此致

阿里

好的,所以我在 ListView 上进行了一些快速谷歌搜索...这是结果。

Imports HtmlAgilityPack

Public Class Form1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
    ListView2.Columns.Add("Participant's contact - Full Name", 250, HorizontalAlignment.Left)
    ListView2.Columns.Add("Brand Name", 100, HorizontalAlignment.Left)
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim web As New HtmlWeb
    Dim doc As HtmlDocument = web.Load("http://www.eurovent-certification.com/en/Certified_products/Access_by_programme.php?rub=04&srub=01&ssrub=&lg=en&select_prog=AHU")
    Dim ParticipantNodes As HtmlNodeCollection = doc.DocumentNode.SelectNodes("/html/body/table/tr/td[2]/table[4]/tr[2]/td[2]//span[@class='texte'] | /html/body/table/tr/td[2]/table[4]/tr[2]/td[2]//a[@class='contenu']")
    Dim ColumnCount As Integer = 1
    Dim TempListItem As New ListViewItem
    For Each item As HtmlNode In ParticipantNodes
        If item.Name = "span" Then
            ColumnCount = 1
            Dim Name As String = item.InnerText.Replace(vbLf, "").Replace(vbCr, "").Replace(vbTab, "")
            TempListItem = ListView1.Items.Add(Name)
        Else
            If Not item.Attributes.Count = 3 Then
                ColumnCount += 1
                If ColumnCount > ListView1.Columns.Count Then
                    ListView1.Columns.Add("Brand Name", 100, HorizontalAlignment.Left)
                End If
                Dim SubName As String = item.InnerText.Replace(vbLf, "").Replace(vbCr, "").Replace(vbTab, "")
                TempListItem.SubItems.Add(SubName)
            End If
        End If
    Next
    Dim breaker = 0
End Sub

End Class

这是输出的一部分:

 [Menu] [AHU]
List of participants to the certification programme for  Air Handling Units (AHU)  :
A.T.C Air Trade Centre Havaland?rma Sistemleri San. ve Tic. Ltd. ?ti. [ATC]
ACS KLIMA Imalat Sanayi ve Ticaret Ltd. Sti [ACS]
AERA Iklimlendirme Teknolojileri San. ve Tic AS [AERA]
AIRCALO [AIRCALO]
AIRLAN INDUSTRIAL S.A. [AIRLAN]
Airtechnic Hatzoudis E.P.E. [AIRTECHNIC]
AL SALEM YORK Manufacturing Co,Ltd [YORK]
AL-KO THERM GmbH [ALKO]
ALARKO CARRIER San. Tic. A.S. [ALARKO] [CARRIER]

我没有经常使用列表视图,所以我不熟悉它,并且由于您有可变数量的列,我会暂时将这些结果存储在某个地方,然后检查我需要制作多少列。 然后将结果转储到列表视图中。