Silverlight configuration values from an external file.

As every silverlight developer knows silverlight stores its own configuration file inside the .XAP file. There falls a requirement and need for the modifications of these configuration files because of different environments and configuration due to different SDLC’s.

Each time we change the environment and there is some modifications in this config we may need to recompile the application according to the need, which will drive us into several unforeseen problems due to differences in environments.One can avoid this problem in 2 ways.

First one is to rename the .XAP file extension to .ZIP, extract the content, modify the config and zip it back to rename back to .XAP.

Above approach is useful when you dont have to pass your application through multiple dev & deployment environments. But this will be problamatic when you develop a solution for enterprise where you application should be deployed for multiple entities and different configurations. Second approach eliminates this by simplifying the process by keeping an additional flat or xml file with all configurations in it, which is easy to modify and deploy to the location.

Lets look at how we can do this: I use XML file generally as configuration file because of XML support in the .Net frameowrk. We will see how we can access an XML config file to silverlight and thus load the configuration information to SL application variables.

Config.xml
<?

xml version="1.0" encoding="utf-8" ?><MySLAppConfig><config key="key1">xyz</config><config key="key2">xyz</config><config key="key3">xyz</config><config key="key4">xyz</config><config key="key5">xyz</config><MySLAppConfig>
Copy this config file into the folder where the .XAP file will be placed.

In the code declare the properties which will be used by the application and should be loaded from this config file.

Create the Properties in App.xaml.cs
private string _Key1;
public string Key1 { get { return _Key1; } set { _Key1 = value; } }
private string _Key2;
public string Key2 { get { return _Key2; } set { _Key2 = value; } }
private string _Key3;
public string Key3 { get { return _Key3; } set { _Key3 = value; } }
private string _Key4;
public string Key4 { get { return _Key4; } set { _Key4 = value; } }
private string _Key5;
public string Key5 { get { return _Key5; } set { _Key5 = value; } }

In the App.xaml use the webclient and download the xml file from location as a string.
Uri url = new Uri("Config.xml", UriKind.Relative);
WebClient client = new WebClient();
client.DownloadStringAsync(url);
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);

Access the values from the event handler code.
try
{
StringReader stream = new StringReader(e.Result);
XmlReader reader = XmlReader.Create(stream);
if (!reader.IsEmptyElement)
{
while (reader.Read())
{
if (reader.MoveToAttribute("key"))
{
switch (reader.Value.ToString())
{
case "Key1":
reader.MoveToElement();
Key1 = reader.ReadElementContentAsString();
break;
case "Key2":
reader.MoveToElement();
Key2 = reader.ReadElementContentAsString();
break;
case "Key3":
reader.MoveToElement();
Key3 = reader.ReadElementContentAsString();
break;
case "Key4":
reader.MoveToElement();
Key4 = reader.ReadElementContentAsString();
break;
case "Key5":
reader.MoveToElement();
Key5 = reader.ReadElementContentAsString();
break;
}
}
}
}
else
{
MessageBox.Show("Unable to Find configuration Params");
}
}
catch
{
MessageBox.Show("Configuration file not found or unable to access it.", "Config error", MessageBoxButton.OK);
}

These property values can be accessed across the application in the following way.
App oApp = Application.Current;
string val1 = oApp.Key1;

This way we have got the flexibility to change the configuration easily and using it in the application.

Happy Coding. :)

About Nagendra

Born and Brought up in Visakhapatnam, Andhra Pradesh. Completed B.E Electrical & Electronics Engineering and currently working in software industry with Energy Domain as Key domain area for which i develop enterprise solutions.
This entry was posted in Silverlight, Technology and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>