2

I am not sure why the callback methods are not fired AT ALL. I am using VS 2010.

 static void Main(string[] args)
        {
            try
            {
                var url = "some link to RSS FEED"; 
                var client = new WebClient();
                client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
                client.DownloadDataCompleted += new DownloadDataCompletedEventHandler(client_DownloadDataCompleted);

                client.DownloadStringAsync(new Uri(url));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message); 
            }
        }
        // THIS IS NEVER FIRED 
        static void client_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
        {
            Console.WriteLine("something"); 
        }

        // THIS IS NEVER FIRED 
        static void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            Console.WriteLine("do something");

            var rss = XElement.Parse(e.Result);

            var pictures = from item in rss.Descendants("channel")
                           select new Picture
                           {
                               Name = item.Element("title").Value
                           };

            foreach (var picture in pictures)
            {
                Console.WriteLine(picture.Name);
                Console.WriteLine(picture.Url);
            }

        }
azamsharp
  • 19,710
  • 36
  • 144
  • 222

2 Answers2

3

The DownloadDataCompleted event is fired if you call the DownloadDataAsync() method. DownloadStringCompleted is fired if you call the DownloadStringAsync() method.

To get the DownloadDataCompleted event to fire, try:

static void Main(string[] args) 
        { 
            try 
            { 
                var url = "http://blog.gravitypad.com";  
                //client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted); 
                client.DownloadDataCompleted += new DownloadDataCompletedEventHandler(client_DownloadDataCompleted); 

                client.DownloadDataAsync(new Uri(url)); 
                Console.ReadLine();
            } 
            catch (Exception ex) 
            { 
                Console.WriteLine(ex.Message);  
            } 
        } 
Paul Kearney - pk
  • 5,435
  • 26
  • 28
  • Should have mentioned: DownloadDataAsync will return the result as a byte array, where DownloadStringAsync will return the result as a string. – Paul Kearney - pk May 08 '10 at 03:33
  • Right, but you are expecting DownloadDataCompleted event to fire. Only the DownloadStringCompleted even will fire... unless you call DownloadDataAsync. – Paul Kearney - pk May 08 '10 at 04:02
  • Sorry - I just realized you are saying *neither* event is firing. When I tested your code, I do get one event or the other to fire... I see "do something" or "something", depending on which method I call. Sorry for the confusion. – Paul Kearney - pk May 08 '10 at 04:07
  • hmm I am not sure why none of the events is firing for me! Thanks for your help! – azamsharp May 12 '10 at 14:40
  • Duh. Had the same issue with DownloadFileCompleted and registering for the wrong events. Thanks for the post. – Jafin Nov 06 '12 at 05:14
1

I had this problem and realized that the uri was not correct. I mean the event wont fire unless the file is read correctly. So I placed my xml file in ClientBin and it worked like magic!

user681593
  • 11
  • 1