Reading files asynchronously using WebClient in Silverlight

No.of Views3358
Bookmarked0 times
By  Dhananjay Kumar   On  16 Apr 2011 11:04:36
Tag : Silver Light and XAML , General
In this article, i will explain how to download or read file asynchronously using WebClient class in Silverlight.The WebClient class is a has many methods,events and properties to support download files in different ways.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at



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.


<?xml version="1.0" encoding="utf-8" ?>
    <Name>John Papa</Name>
    <Name>John Papa</Name>
    <Name>Scott Gui</Name>
    <Name>Jessy Liberty</Name>
    <Name>Tim Huer</Name>
    <Name>Victor Gud</Name>
    <Name>Machesh Chand</Name>
    <Name>Pinal Dave</Name>
    <Name>Suprotim Agarwal</Name>
    <Name>Dhananjay Kumar</Name>
    <Name>Kunal Chawudhary</Name>
    <Name>Abhijit Jana</Name>
    <Name>Shiv Prasad Koirala</Name>

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.


<UserControl xmlns:sdk="" 
    d:DesignHeight="300" d:DesignWidth="400">
    <Grid x:Name="LayoutRoot" Background="White">
        <StackPanel Orientation="Vertical" Margin="50,50,50,50">
        <Button x:Name="btnDemo" Content="Click To get Data From  XML using WebClient" Height="62" Width="362" />
            <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" Height="200" Width="300">
                <TextBlock x:Name="txtDataFromXml"  Width="Auto" Height="Auto" />

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

Image Loading

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. 

Image Loading

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();


Image Loading

That's all look good and hope helps.thank you for reading.

Sign Up to vote for this article
About Author
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Joined date-20 Jul 2009
Home Page-
Blog Page-
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at . You can follow him and reach him at
Other popularSectionarticles
    In this article we’ll have a brief introduction to Microsoft Silverlight, see how it fits with other technologies today, and watch it in action. Get ready!
    Published Date : 05/May/2011
    When you start learning Silverlight and create your first Silverlight application, one thing you will notice XAP file along with HTML and ASPX files. First time after running your Silverlight application you can see XAP file in client bin folder. Before running of Silverlight application, you will not find XAP file in client bin folder.
    Published Date : 19/Apr/2011
    This article will give a basic introduction of behavior feature in SILVERLIGHT3.0. I will also walkthrough to create a custom behavior.
    Published Date : 05/Apr/2010
    we will see how we can filter a Textbox on Keyboard inputs. We will see how we can block Numeric input from Keyboard.
    Published Date : 16/May/2010
    In this article we would see how we can use ADO.NET Entity Data Model as the ORM in Silverlight 3 Application, and ADO.NET Data Service to fetch the data.
    Published Date : 20/Apr/2010
There is no comments for this articles.
Leave a Reply
Display Name:
(not display in page for the security purphase)
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top