Results 1 to 7 of 7

Thread: [RESOLVED] DownloadStringASync-Somethings wrong

  1. #1

    Thread Starter
    Addicted Member Mal1t1a's Avatar
    Join Date
    Mar 2008
    Posts
    157

    Resolved [RESOLVED] DownloadStringASync-Somethings wrong

    Hi, This is an old code I have dug up, and I do remember it working, at least I think it was this exact code. Anyways, I'm trying to download a WebPage as a String, which works, however, I can not parse each line of Html at a time. I can either, Parse each Character, or parse the whole thing as a single String. Here's my code:

    Code:
    Public Class Form1
        Private WithEvents WC As New Net.WebClient
    
        Private Sub WC_StringFinished(ByVal sender As System.Object, ByVal e As Net.DownloadStringCompletedEventArgs) Handles WC.DownloadStringCompleted
            Dim htmlLines() As String = e.Result.Split(New String() {Environment.NewLine}, StringSplitOptions.None)
            For Each i As String In htmlLines
            ComboBox1.Items.Add(i)
            Next
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                WC.DownloadStringAsync(New Uri("http://www.google.ca/search?hl=en&q=Mal1t1a&btnG=Google+Search&meta=&aq=f&oq="))
        End Sub
    End Class

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: DownloadStringASync-Somethings wrong

    Are you sure that the lines are delimited by the value you're specifying? Environment.NewLine will return a carriage return and line feed pair on a Windows system, i.e. CR LF. Quite possibly the file you're reading has lines delimited by line feeds only. If you want to be able to handle both, try creating a StringReader and calling ReadLine.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member Mal1t1a's Avatar
    Join Date
    Mar 2008
    Posts
    157

    Re: DownloadStringASync-Somethings wrong

    Well, all I'm trying to accomplish, is the read the HTML Page, line-by-line. It's worked before, and well, now it doesn't.

  4. #4
    Fanatic Member
    Join Date
    Mar 2008
    Posts
    519

    Re: DownloadStringASync-Somethings wrong

    Create a StringReader, as jmcilhinney said.
    That way you can read the code line by line.

    And are you sure that the StringFinished event is called when the download is complete?
    Try adding a MsgBox saying something to be sure that the event gets raised.

  5. #5

    Thread Starter
    Addicted Member Mal1t1a's Avatar
    Join Date
    Mar 2008
    Posts
    157

    Re: DownloadStringASync-Somethings wrong

    Yes, I'm positive it's being called. I can try creating a StringReader as he said, however, it feels odd, because I've never needed to do that before. I'm pretty sure this code has sufficed for me.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: DownloadStringASync-Somethings wrong

    It doesn't matter what you're trying to accomplish; if you don't use the right code then it won't work. The fact that some code worked for one file doesn't necessarily mean it will work for another file because the two files may be different. As I said in my previous post, you are explicitly try to split the file on CR/LF pairs. If the lines of the file are delimited by LF characters alone then there are no CR/LF pairs so the file won't be split. You can test that by trying to split that file on LF characters alone:
    Code:
    Dim htmlLines() As String = e.Result.Split(ControlChars.Lf)
    If that works where your other code doesn't then that is the issue for that particular file.

    As I also said in my previous post, if you want to be able to handle files that are delimited in both ways then you should use a StringReader.
    Last edited by jmcilhinney; Dec 25th, 2009 at 08:24 PM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Addicted Member Mal1t1a's Avatar
    Join Date
    Mar 2008
    Posts
    157

    Resolved Re: DownloadStringASync-Somethings wrong

    Quote Originally Posted by jmcilhinney View Post
    It doesn't matter what you're trying to accomplish; if you don't use the right code then it won't work. The fact that some code worked for one file doesn't necessarily mean it will work for another file because the two files may be different. As I said in my previous post, you are explicitly try to split the file on CR/LF pairs. If the lines of the file are delimited by LF characters alone then there are no CR/LF pairs so the file won't be split. You can test that by trying to split that file on LF characters alone:
    Code:
    Dim htmlLines() As String = e.Result.Split(ControlChars.Lf)
    If that works where your other code doesn't then that is the issue for that particular file.

    As I also said in my previous post, if you want to be able to handle files that are delimited in both ways then you should use a StringReader.
    Thank-you, That worked perfectly, also, sorry, I had to Re-Download and Install Everything. I don't exactly understand how to tell. Would I just try both of them? Or is there a certain way to actually tell? Also, thanks again!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width