Using XmlReader in Silverlight
This is written for Silverlight v2 beta 2.
In this tutorial I will show you how to populate an XmlReader from an XML file on your server. This is the easiest way I have found to do simple data access when hosting your Silverlight projects on a Linux box.
If you are new to Silverlight even if you are not new to C# things are a little different. One of the biggest sources of irritation for me while writing my first Silverlight control was getting a simple XmlReader loaded with data. If you are a traditional C# developer you might think all you need to do is call XmlReader reader = XmlReader.Create("xml_file.xml"), but that would be entirely too easy. To use an XML file located on your server you need to first download it into your application’s XAP file at runtime.
To make this happen, the first thing to do is find the public Page() method in your Silverlight project and paste the code below. This will start the process of downloading the XML file into your XAP package.
*Note* if you want to debug your code in Visual Studio copy your xml file (path/xml_file.xml) to the ClientBin directory locally. This will allow you to step through your code and debug.
After Page() has run and once it has finished downloading your XML file it will call client_DownloadStringCompleted and execute the method below. The XML will be loaded up in e.Result. This can get confusing if you are not use to making Async calls.
Now we have an XmlReader loaded with data!
In this tutorial I will show you how to populate an XmlReader from an XML file on your server. This is the easiest way I have found to do simple data access when hosting your Silverlight projects on a Linux box.
If you are new to Silverlight even if you are not new to C# things are a little different. One of the biggest sources of irritation for me while writing my first Silverlight control was getting a simple XmlReader loaded with data. If you are a traditional C# developer you might think all you need to do is call XmlReader reader = XmlReader.Create("xml_file.xml"), but that would be entirely too easy. To use an XML file located on your server you need to first download it into your application’s XAP file at runtime.
To make this happen, the first thing to do is find the public Page() method in your Silverlight project and paste the code below. This will start the process of downloading the XML file into your XAP package.
*Note* if you want to debug your code in Visual Studio copy your xml file (path/xml_file.xml) to the ClientBin directory locally. This will allow you to step through your code and debug.
public Page()
{
InitializeComponent();
WebClient client = new WebClient ();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
Uri url = new Uri("path/xml_file.xml", UriKind.Relative);
client.DownloadStringAsync(url);
}
{
InitializeComponent();
WebClient client = new WebClient ();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
Uri url = new Uri("path/xml_file.xml", UriKind.Relative);
client.DownloadStringAsync(url);
}
After Page() has run and once it has finished downloading your XML file it will call client_DownloadStringCompleted and execute the method below. The XML will be loaded up in e.Result. This can get confusing if you are not use to making Async calls.
void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
{
StringReader stream = new StringReader(e.Result);
XmlReader reader = XmlReader.Create(stream);
}
}
{
if (e.Error == null)
{
StringReader stream = new StringReader(e.Result);
XmlReader reader = XmlReader.Create(stream);
}
}
Now we have an XmlReader loaded with data!


5 Comments:
I have been digging around this for a day now when I found your post.
cheers
//Christoffer
Can u tell me where to put xml exactly in the package as I am getting root element is missing error and xml is well formed though.
Thanks,
-Lil
Check to make sure your XML string is not blank just before loading your reader.
Now my RSS feed aggregator works! http://www.codehandyman.com