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

html html-agility-pack html-table selectnodes vb.net

Question

I am trying to get all participant name value from the list with (span class=text) and also their shortname value with (class=contenu) and add them to the different coloumn of the listview item which will shown as similiart to the web page. The user can see the full name of the participant and also can click the individual participant brand (short name) to get more detail.

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

Try to simplify the problem ;

This is the part from the full page which shows participant names and their brands ( some of them has one and some other has two, three or four)

like below ; Mekar srl (Participant Name) has 4 different brand which are separated with comma ( Eden, Mekar VENCO, Venticlima)

MEKAR s.r.l. ( EDEN , MEKAR , VENCO , VENTILCLIMA )

but some of them only one ;

like below example ; Menegra Gmbh ( participant name) has only one brand as Menegra MENERGA GmbH ( Menerga )

So i will try to write these data almost the same logic with the page to a listview ;

1st column will be listing participant name and with the same row at the second column brand name will be listed ( or if more then one, third and fourth column of the same row)

Regardless from the listview i need to get idea to hold the data that i get from the webpage to record at the array type string and show them at the listview later.

With the code above i am reading all the nodes continuosly and lost the relations between them ( like participant name with multiple brand)

Hope this statement will be more clear which i also hope again to get more support

Regards

Ali

Accepted Answer

Ok so I did some quick googling on ListView...here are the results.

enter image description here

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

This is part of the output:

 [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]

I haven't used listview that much so I'm not familiar with it, and since you have a variable amount of columns i would store these results somewhere temporarily then check how many columns I need to make. Then just dump the results in listview.




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why