htmlvb.netstringlistboxhtml-agility-pack

vb.net from string to listbox line by line


i made an webrequestto get an htmlcode of an website and then i extract the the wanted links with htmlagilitypack like this :

    'webrequest'
    Dim rt As String = TextBox1.Text
    Dim wRequest As WebRequest
    Dim WResponse As WebResponse
    Dim SR As StreamReader
    wRequest = FtpWebRequest.Create(rt)
    WResponse = wRequest.GetResponse
    SR = New StreamReader(WResponse.GetResponseStream)
    rt = SR.ReadToEnd
    TextBox2.Text = rt

    'htmlagility to extract the links'
    Dim htmlDoc1 As New HtmlDocument()
    htmlDoc1.LoadHtml(rt)
    Dim links = htmlDoc1.DocumentNode.SelectNodes("//*[@id='catlist-listview']/ul/li/a")
    Dim hrefs = links.Cast(Of HtmlNode).Select(Function(x) x.GetAttributeValue("href", ""))
    'join the `hrefs`, separated by newline, into one string'
    textbox3.text = String.Join(Environment.NewLine, hrefs)

the links are like this :

http://wantedlink1 
http://wantedlink2 
http://wantedlink3 
http://wantedlink4
http://wantedlink5
http://wantedlink6
http://wantedlink7

Now i want to add every line in the string to listbox instead of textbox
one item for each line

THERE IS ABOUT 400 http://wantedlink


Solution

  • hrefs in your case already contained IEnumerable(Of String). Joining them into one string and then split it again to make it work is weird. Since String.Split() returns array, maybe you only need to project hrefs into array to make .AddRange() to work :

    ListBox1.Items.AddRange(hrefs.ToArray())