|
-
May 1st, 2007, 06:25 AM
#1
Thread Starter
Lively Member
[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:
Public Class XmlSource
Inherits System.Net.WebClient
Private _name As String
Private _uri As Uri
Private _lasterror As String
Private _xmldocstring As String
Private _xmldoc As System.Xml.XmlDocument
...snip snip...
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Protected Overrides Sub OnDownloadFileCompleted(ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
' Preferable I would like to handle the e.Result presently in wcDownloadStringCompleted()
' so I can create the XmlTextReader and all that stuff.
MyBase.OnDownloadDataCompleted(e)
End Sub
Public Sub fetchXML()
Dim wcWebClient As New System.Net.WebClient 'WebClient
' Setup Event Handlers
AddHandler wcWebClient.DownloadStringCompleted, AddressOf wcDownloadStringCompleted
' AddHandler wcWebClient.DownloadProgressChanged, AddressOf weDownloadProgressChanged
'Fetch HTML page
wcWebClient.DownloadStringAsync(_uri)
End Sub
' This functionality is currently handled by the main form but I would prefer to handle that within
' this Class, before the MyBase.OnDownloadDataCompleted(e) is called.
Private Sub wcDownloadStringCompleted(ByVal sender As Object, ByVal e As System.Net.DownloadStringCompletedEventArgs)
'If the string request went as planned and wasn't cancelled:
If e.Cancelled = False AndAlso e.Error Is Nothing Then
'Use e.Result to get the String
Dim sr As New System.IO.StringReader(CStr(e.Result))
' The XmlTextReader
Dim xr As New System.Xml.XmlTextReader(sr)
' The XmlDocument
_xmldoc = New System.Xml.XmlDocument
_xmldoc.Load(xr)
_lasterror = String.Empty
Else
_lasterror = CStr(e.Error.Message)
End If
End Sub
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.
-
May 1st, 2007, 06:53 AM
#2
Re: [2005] WebClient derived class issues
This line:
vb Code:
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.
-
May 1st, 2007, 07:10 AM
#3
Thread Starter
Lively Member
Re: [2005] WebClient derived class issues
 Originally Posted by jmcilhinney
This line:
vb Code:
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.
-
May 1st, 2007, 08:52 AM
#4
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?
-
May 1st, 2007, 02:40 PM
#5
Thread Starter
Lively Member
Re: [2005] WebClient derived class issues
 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.
-
May 1st, 2007, 02:58 PM
#6
Thread Starter
Lively Member
Re: [2005] WebClient derived class issues
I now have this, it seems to make a lot more sense to me.
=vb Code:
Protected Overrides Sub OnDownloadStringCompleted(ByVal e As System.Net.DownloadStringCompletedEventArgs)
'If the string request went as planned and wasn't cancelled:
If e.Cancelled = False AndAlso e.Error Is Nothing Then
'Use e.Result to get the String
Dim sr As New System.IO.StringReader(CStr(e.Result))
' The XmlTextReader
Dim xr As New System.Xml.XmlTextReader(sr)
' The XmlDocument
_xmldoc = New System.Xml.XmlDocument
_xmldoc.Load(xr)
End If
MyBase.OnDownloadStringCompleted(e)
End Sub
Public Sub fetchXML()
'Fetch HTML page
MyBase.DownloadStringAsync(_uri)
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|