SUPPORTERS:

FlashComponents
  Flash Templates
  Flash Gallery
  Flash Slideshow
  Flash Menu
  Flash Design
  Flash Video
  User Interface

  

 

 

 

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. 


I hope the information helped. If you have any questions or comments, please don't hesitate to post them on the kirupa.com Forums. Just post your question and I, or our friendly forum helpers, will help answer it.

The following is a list of related tutorial and help resources that you may find useful:

How to use the Forums
New, Upcoming, and In-Progress Tutorials
How to Help out kirupa.com
Writing Tutorials
 

Cheers!

Kirupa Chinnathambi
about | facebook | twitter

 

 

1 | 2 | 3 | 4

SHARE:

SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple. Creative web apps. Make your own free flash banners and photo slideshows.
Buy or sell stock flash, video, audio and fonts for as little as 50 cents at FlashDen.

Flash Transition Effects

Flash Effect Tutorials

Digicrafts Components Flash effects. Art without coding.
Flipping Book - page flip flash component. Flash-Gallery.com - Get your flash photo gallery (flash component or swf gallery
Everythingfla - HD video school membership starts as low as 4.99 a month for the first 3 months. New Videos  updated weekly. current focus is Learning how to develop in AS3. Learn how to advertise on kirupa.com
 

cdn
content delivery network (cdn)