Get Microsoft Silverlight

Saturday, March 07, 2009

Different ways to access XML data in Silverlight Application

One of the most common requirement in the Silverlight development is to load XML data in to the application. I am going to describe a few different options that we can choose based on the situations. I am using LINQ for the XML parsing in the examples. Taking a sample XML file names Sample.xml with the following content

<Employees>
<Employee Name="John"/>
<Employee Name="Mark"/>
<Employee Name="David"/>
</Employees>

1. Load XML file directly from the Web Server hosted Silverlight XAP

You can host the XML file at your web server itself and Silverlight application can load it on demand. To simulate this in Visual Studio you need to create a Test Website for your Silverlight application and put xml file into the ClientBin folder.

image

Now we can write few lines of code to read the XML. WebClient will download the file asynchronously

void Page_Loaded(object sender, RoutedEventArgs e)
{
WebClient xmlClient = new WebClient();
xmlClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(XMLFileLoaded);
xmlClient.DownloadStringAsync(new Uri("Sample.xml", UriKind.RelativeOrAbsolute));
}
void XMLFileLoaded(object sender, DownloadStringCompletedEventArgs e)
{
string XmlString = e.Result; // Got all your XML data in to a string
XDocument elem = XDocument.Load(XmlReader.Create(new StringReader(XmlString)));
var collection = from nod in elem.Descendants("Employee")
select nod.Attribute("Name").Value;
lstBox.ItemsSource = collection ; // XML data bind to the UI
}

2. Load XML file directly from the Silverlight App(XAP)

image

Add the XML file as a content to the Silverlight project. This is the easiest way to build and deploy a Silverlight project, since you really don't need to worry about the XML file once it is placed as part of the project. When you compile, it will automatically embed the XML file inside the XAP file. But the big problem is that you can’t easily change the XML data after the deployment. . And here the C# to read data from this situation

 XDocument elem = XDocument.Load("Sample.xml");
var collection = from nod in elem.Descendants("Employee")
select nod.Attribute("Name").Value;

3. Load XML file from one of the Silverlight Assemblies

When you have more than one Silverlight class libraries associated with a Silverlight application, it may be meaningful to add the XML file to a different project than the main project. So this case you need to add the file as an EmbeddedResource.

image

  Stream stream = this.GetType().Assembly.GetManifestResourceStream("XmlLoadingTest.Sample.xml");
XDocument elem = XDocument.Load(XmlReader.Create(stream));
var coll = from nod in elem.Descendants("Employee")
select nod.Attribute("Name").Value;

4. Load XML file from a URI

This is the case of accessing third party data from feeds and services. It is a big security threat to access resources from a server where the XAP file didn't not originate. So the server you are requesting access needs a special permission files hosted. You can read more about this here-Making a Service Available Across Domain Boundaries) and here-Network Security Access Restrictions in Silverlight 2

When coming to read this kind of hosted XML urls from C#, you can use either step(1) or step(2) of the above C# code snippets. I prefer the step(1) option because it is asynchronous and will not hang up the UI. And also it will not make the XAP size larger with larger XML files, so the initial loading will be faster.

Sunday, March 01, 2009

Sunny Orange - WPF Theme 1

Recently I found out that I have an old blog post about Control Templating a Button, getting a lot of hits. Many out there are looking for free control templates and themes for WPF/Silverlight applications. I am planning to publish few WPF/Silverlight themes in the coming posts. So here I am publishing a simple theme which has almost similar colors to the old blog post. It contains very basic set of controls. 
1)Button
2)ToggleButton
3)ComboBox
4)TextBox
5)CheckBox
6)RadioButton
7)ScrollBar
8)ScrollViewer
9)ListBox
10)ProgressBar
11)Slider

You can find the source XAML style here - SunnyOrange_Theme.xaml
WPF control theming and templating is very interesting and fun task in the WPF application development, it took only an hour or two for me to create this theme file. Please let me know your valuable feedbacks and suggestions .