.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
david stephan
Gaurav Pal

Home >> Articles >> Silverlight >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Asynchronously Reading files using WebClient class in Silverlight

Posted By:Dhananjay Kumar       Posted Date: March 06, 2011    Points: 200    Category: Silverlight    URL: http://www.dotnetspark.com  

Reading files asynchronously using WebClient class in Silverlight. WebClient class is used in Silverlight to asynchronously download or read a file from a particular URI

WebClient class is used in Silverlight to asynchronously download or read a file from a particular URI.

1.  WebClient class is under System.Net namespace.
2.  This can retrieve data from any format of service. This can retrieve data in JSON, POX, and REST etc. format.
3.  WebClient makes the entire request asynchronously. So it does not block any other operation.
4.  On basis of data type of response from URI, we can choose class to parse the response data.  If response is JOSN, we can use DataContractJSONSerliazer to parse response data.
5.  Any type of file including Media, Images, and XML etc. can be downloaded using WebClient class.
Let us read a XML file asynchronously using WebClient class.
1.  Add XML file named Data.xml in the client bin folder. Right click on client Bin folder and add a new item then select XML file from Data tab. Give name of the file as Data.xml. 


    John Papa
    John Papa
    Scott Gui
    Jessy Liberty
    Tim Huer
    Victor Gud
    Machesh Chand
    Pinal Dave
    Suprotim Agarwal
    Dhananjay Kumar
    Kunal Chawudhary
    Abhijit Jana
    Shiv Prasad Koirala

2.   Once Data.xml file is in place inside client bin folder, let us design XAML page.  We will put one Button and one TextBlock.
On click event of button, we will make asynchronous call to read Data.xml file. After successful reading, we will bind xml response to TextBlock. 



3.  On click event of button, we will read Data.xml file asynchronously.

At time of creation of URL either we can provide absolute URI or relative.  In our example Data.xml file is in client bin folder so we are providing relative address.
In OpenReadCompleted event handler, we will parse XML file and bind response to TextBlock.

For reference full code is given below,


using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
namespace SilverlightApplication5
    public partial class MainPage : UserControl
        public MainPage()
            btnDemo.Click +=new RoutedEventHandler(btnDemo_Click);
        private void btnDemo_Click(object sender, RoutedEventArgs e)
            WebClient client = new WebClient();
            Uri uritoXML = new Uri("Data.xml",UriKind.Relative);           
            client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
        void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
            Stream s = e.Result;
            StreamReader strReader = new StreamReader(s);
            txtDataFromXml.Text = strReader.ReadToEnd();



 Subscribe to Articles


Further Readings:

Author: Mrinmoy         Company URL: http://www.dotnetspark.com
Posted Date: May 26, 2011

Dhananjay: How do I display just one name from the XML file?

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend