Wednesday, June 18, 2008
Access external XML files in Silverlight using PHP
This is written for Silverlight v2 beta 2.

Silverlight only allows you to use Relative paths when loading XML files. That means the XML file has to be on your server. In this tutorial I will show you how to access an Absolute path or an external XML file.

In order to do this we will need to write an XML proxy page in PHP. Our PHP page will need to take the XML contents from an external XML page and render it as its own. Then your Silverlight application can simply connect that our XML proxy page for its data.

If you would like to know how to load the XML into an XmlReader you can read my previous entry Using XmlReader in Silverlight.

If we wanted to be slick, we can take in the XML path dynamically through the query string so we can reuse our XML proxy page in other Silverlight applications that require access to external XML files. Luckily this is very easy to do in PHP. Just copy this code into a file called xml_proxy.php.

<?php
header('Content-type: text/xml');

if (isset($_GET['XML']))
   echo(file_get_contents($_GET['XML']));
else
   echo('<Data>No Input Entered</Data>');
?>


*note* Some hosts do not support file-access, like mine. However, I was able to get around this by using the following code.

<?php
header('Content-type: text/xml');

if (isset($_GET['XML']))
{
   $c = curl_init();
   curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($c, CURLOPT_URL, $_GET['XML']);
   $contents = curl_exec($c);
   curl_close($c);

   if (!$contents)
      $contents = '<Error>Could not load file</Error>';

   echo($contents);
}
else
   echo('<Data>No Input Entered</Data>');
?>


Here is an example of my xml proxy page. http://silverlight.whatisreal.com/xml_proxy.php?XML=http://rss.slashdot.org/Slashdot/slashdot.

In your Silverlight application change the Uri path to the example from above.

Uri url = new Uri("xml_proxy.php?XML=http://rss.slashdot.org/Slashdot/slashdot", UriKind.Relative)


That is all there is to it.
Tuesday, June 17, 2008
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.

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);
}


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);
   }
}


Now we have an XmlReader loaded with data!