Loading an XML File into Silverlight - Page 4
       by kirupa  |  27 April 2008

In the previous page, you learned about all of the code that we use for actually loading our XML file into your application. One of the lines of code that you saw was one where you associated an event handler with your WebClient object's DownloadStringCompleted event:

private void LoadXMLFile()
{
WebClient xmlClient = new WebClient();
xmlClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(XMLFileLoaded);
xmlClient.DownloadStringAsync(new Uri("sampleXML.xml", UriKind.RelativeOrAbsolute));
}

The event handler you specified was called XMLFileLoaded. Let's look at that XMLFileLoaded event handler and see how it is used to actually read your XML content:

void XMLFileLoaded(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
{
string xmlData = e.Result;
HtmlPage.Window.Alert(xmlData);
}
}

The above event handler, which I will just call as the XMLFileLoaded method from now on, gets called immediately once your XML file has successfully been downloaded. The various properties related to that download are passed in via the DownloadStringCompletedEventArgs object represented as e that you see in the method signature.


One of the values e provides access to is any error that was reported during the download. That is why on the first line, I check to make sure that no error was reported:

void XMLFileLoaded(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
{
string xmlData = e.Result;
HtmlPage.Window.Alert(xmlData);
}
}

I check for null because all I want to know is that no error was provided. If I did want to check for the exact type of error and react appropriately, the Error property takes in objects of type Exception. Replacing null with the appropriate Exception and message would give you the granularity in error handling that you may want.


void XMLFileLoaded(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
{
string xmlData = e.Result;
HtmlPage.Window.Alert(xmlData);
}
}

Probably the most important value our DownloadStringCompletedEventArgs object e provides is the Result property which takes what you downloaded and returns that content as a string.


void XMLFileLoaded(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
{
string xmlData = e.Result;
HtmlPage.Window.Alert(xmlData);
}
}

This line is not going to be important for you, but I will explain it anyway! I want to display a browser alert that contains my downloaded data and presents it to you...in the most annoying way possible. The above line allows you to do that.


If you are stuck somewhere, feel free to download the source file to run it all on your own machine:

Download Final Source

The above solution is a Visual Studio 2008 project. Make sure you have the Silverlight Tools installed as well. My Getting Started guide should help you out. 


Just a final word before we wrap up. If you have a question and/or want to be part of a friendly, collaborative community of over 220k other developers like yourself, post on the forums for a quick response!

Kirupa's signature!

 

 

1 | 2 | 3 | 4




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.