Results 1 to 6 of 6

Thread: [2005] WebClient derived class issues

  1. #1

    Thread Starter
    Lively Member mightor's Avatar
    Join Date
    Apr 2007
    Location
    Turn around, I'm right behind you...
    Posts
    99

    [2005] WebClient derived class issues

    Hi there,

    I am attempting to write a new class called XmlSource that inherits from System.Net.WebClient. Basically, I want to override the OnDownloadFileCompleted function to process the System.Net.DownloadStringCompletedEventArgs.Result passed to the eventhandler for this event. If this sounds confusing, maybe the comments in my code will help.

    =vb Code:
    1. Public Class XmlSource
    2.     Inherits System.Net.WebClient
    3.  
    4.     Private _name As String
    5.     Private _uri As Uri
    6.     Private _lasterror As String
    7.     Private _xmldocstring As String
    8.     Private _xmldoc As System.Xml.XmlDocument
    9.  
    10. ...snip snip...
    11.  
    12.     Public Property Name() As String
    13.         Get
    14.             Return _name
    15.         End Get
    16.         Set(ByVal value As String)
    17.             _name = value
    18.         End Set
    19.     End Property
    20.  
    21.     Protected Overrides Sub OnDownloadFileCompleted(ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
    22.         ' Preferable I would like to handle the e.Result presently in wcDownloadStringCompleted()
    23.         ' so I can create the XmlTextReader and all that stuff.
    24.         MyBase.OnDownloadDataCompleted(e)
    25.     End Sub
    26.  
    27.     Public Sub fetchXML()
    28.         Dim wcWebClient As New System.Net.WebClient     'WebClient
    29.         ' Setup Event Handlers
    30.         AddHandler wcWebClient.DownloadStringCompleted, AddressOf wcDownloadStringCompleted
    31.         ' AddHandler wcWebClient.DownloadProgressChanged, AddressOf weDownloadProgressChanged
    32.  
    33.         'Fetch HTML page
    34.         wcWebClient.DownloadStringAsync(_uri)
    35.     End Sub
    36.  
    37.     ' This functionality is currently handled by the main form but I would prefer to handle that within
    38.     ' this Class, before the MyBase.OnDownloadDataCompleted(e) is called.
    39.     Private Sub wcDownloadStringCompleted(ByVal sender As Object, ByVal e As System.Net.DownloadStringCompletedEventArgs)
    40.         'If the string request went as planned and wasn't cancelled:
    41.         If e.Cancelled = False AndAlso e.Error Is Nothing Then
    42.             'Use e.Result to get the String
    43.             Dim sr As New System.IO.StringReader(CStr(e.Result))
    44.  
    45.             ' The XmlTextReader
    46.             Dim xr As New System.Xml.XmlTextReader(sr)
    47.  
    48.             ' The XmlDocument
    49.             _xmldoc = New System.Xml.XmlDocument
    50.             _xmldoc.Load(xr)
    51.  
    52.             _lasterror = String.Empty
    53.         Else
    54.             _lasterror = CStr(e.Error.Message)
    55.         End If
    56.     End Sub
    57.  
    58.  
    59. End Class

    Maybe my code somewhat explains it. The idea is that once the download is completed, and the DownloadStringCompleted event has been raised to the application, the xmldocuement is ready for consumption by simply asking for it through a property (to be written, still).

    So my question is, how do I access the parameter passed to the eventhandler BEFORE it is passed there so I can fiddle with the DownloadStringCompletedEventArgs.Result.

    Thanks,
    Mightor
    What the world needs is more geniuses with humility, there are so few of us left.
    If my post was accidentally useful, please rate it as such, thanks!
    Mon aéroglisseur est plein des anguilles.

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

    Re: [2005] WebClient derived class issues

    This line:
    vb Code:
    1. MyBase.OnDownloadDataCompleted(e)
    is calling the base implementation of the method, which is where the event gets raised. If you edit the properties of the 'e' parameter before that line then those updated property values will be appear in all handlers for that event.
    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
    Lively Member mightor's Avatar
    Join Date
    Apr 2007
    Location
    Turn around, I'm right behind you...
    Posts
    99

    Re: [2005] WebClient derived class issues

    Quote Originally Posted by jmcilhinney
    This line:
    vb Code:
    1. MyBase.OnDownloadDataCompleted(e)
    is calling the base implementation of the method, which is where the event gets raised. If you edit the properties of the 'e' parameter before that line then those updated property values will be appear in all handlers for that event.
    Yeah, that is what I thought would do the trick as well. However, the "e" (AsyncCompletedEventArgs) in that function is not the same kind of "e" (DownloadStringCompletedEventArgs) that contains the .Result attribute I need
    Any idea how I can get access to the DownloadStringCompletedEventArgs before it is passed to the handler?

    Thanks,
    Mightor
    What the world needs is more geniuses with humility, there are so few of us left.
    If my post was accidentally useful, please rate it as such, thanks!
    Mon aéroglisseur est plein des anguilles.

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

    Re: [2005] WebClient derived class issues

    There is no DownloadStringCompletedEventArgs object associated with the DownloadFileCompleted event. When a string download operation completes the WebClient creates a DownloadStringCompletedEventArgs object and raises the DownloadStringCompleted event. The Result property you're talking about specifically contains the string that was downloaded. When a file download operation completes the WebClient creates a AsyncCompletedEventArgs object and raises the DownloadFileCompleted event. There was no string downloaded so there is no reason to a have a Result property. If there's no string then what could that property possibly be for?
    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

  5. #5

    Thread Starter
    Lively Member mightor's Avatar
    Join Date
    Apr 2007
    Location
    Turn around, I'm right behind you...
    Posts
    99

    Re: [2005] WebClient derived class issues

    Quote Originally Posted by jmcilhinney
    There is no DownloadStringCompletedEventArgs object associated with the DownloadFileCompleted event. When a string download operation completes the WebClient creates a DownloadStringCompletedEventArgs object and raises the DownloadStringCompleted event. The Result property you're talking about specifically contains the string that was downloaded. When a file download operation completes the WebClient creates a AsyncCompletedEventArgs object and raises the DownloadFileCompleted event. There was no string downloaded so there is no reason to a have a Result property. If there's no string then what could that property possibly be for?
    Damn you and your makingsensedness! I will be sure to flagellate myself to make up for this rather silly mistake. Thanks for pointing that out to me. I will try again and let you know how it went.

    Gr,
    Mightor
    What the world needs is more geniuses with humility, there are so few of us left.
    If my post was accidentally useful, please rate it as such, thanks!
    Mon aéroglisseur est plein des anguilles.

  6. #6

    Thread Starter
    Lively Member mightor's Avatar
    Join Date
    Apr 2007
    Location
    Turn around, I'm right behind you...
    Posts
    99

    Re: [2005] WebClient derived class issues

    I now have this, it seems to make a lot more sense to me.
    =vb Code:
    1. Protected Overrides Sub OnDownloadStringCompleted(ByVal e As System.Net.DownloadStringCompletedEventArgs)
    2.         'If the string request went as planned and wasn't cancelled:
    3.         If e.Cancelled = False AndAlso e.Error Is Nothing Then
    4.             'Use e.Result to get the String
    5.             Dim sr As New System.IO.StringReader(CStr(e.Result))
    6.  
    7.             ' The XmlTextReader
    8.             Dim xr As New System.Xml.XmlTextReader(sr)
    9.  
    10.             ' The XmlDocument
    11.             _xmldoc = New System.Xml.XmlDocument
    12.             _xmldoc.Load(xr)
    13.         End If
    14.         MyBase.OnDownloadStringCompleted(e)
    15.     End Sub
    16.  
    17.     Public Sub fetchXML()
    18.         'Fetch HTML page
    19.         MyBase.DownloadStringAsync(_uri)
    20.     End Sub
    I need to change a bunch of code in my main app to actually test this but at least I am not getting any kind of funny parse errors.

    Gr,
    Mightor
    What the world needs is more geniuses with humility, there are so few of us left.
    If my post was accidentally useful, please rate it as such, thanks!
    Mon aéroglisseur est plein des anguilles.

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