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