MINI SUPPORTERS:

 

 

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. 


Need Help?

If you have questions, need some assistance on this topic, or just want to chat - please drop by our friendly forums and post your question. There are a lot of knowledgeable and witty people who would be happy to help you out. Plus, we have a large collection of smileys you can use

Share

Did you enjoy reading this and found it useful? If so, please share it with your friends:

If you didn't like it, I always like to hear how I can do better next time. Please feel free to contact me directly at kirupa[at]kirupa.com.

Cheers!

Kirupa Chinnathambi
about | facebook | twitter

 

 

1 | 2 | 3 | 4

SUPPORTERS:

cloud storage
cloud storage
kirupa.com's fast and reliable hosting provided by Media Temple. Creative web apps. Make your own free flash banners and photo slideshows.
HTML5 CSS3 Mobile Gallery for iPhone, iPad Flash effects. Art without coding.
Flipping Book - page flip flash component. Flash-Gallery.com - Get your flash photo gallery (flash component or swf gallery
X-Platform Application Development for Flash Free Flash Components Download - XML Templates, Players and Galleries.

two computer monitors

US Direct

Learn how to advertise on kirupa.com  
 
SHARE:



MINI SUPPORTERS: