kirupa.com - Silverlight Tutorial: 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: 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.  Got a question or just want to chat? Comment below or drop by our forums (they are actually the same thing!) where a bunch of the friendliest people you'll ever run into will be happy to help you out! When Kirupa isn’t busy writing about himself in 3rd person, he is practicing social distancing…even on his Twitter, Facebook, and LinkedIn profiles. Hit Subscribe to get cool tips, tricks, selfies, and more personally hand-delivered to your inbox.    

This is a companion discussion topic for the original entry at https://www.kirupa.com/blend_silverlight/loading_xml_sl2_pg4.htm