XMLPreprocess Getting Started


XMLPreprocess is a simple command line utility that can be used to deploy config files to multiple environments without having to maintain multiple copies. It can be used as a custom action in your MSI to transform the files as they are being deployed to different environments.

The goal of this tool was to minimize the maintenance of multiple configuration files for deployment and I should say it neatly meets the goal.  To understand it better – a single config file can be written and maintained as the source of truth, and it can be decorated with non-breaking comments that contain the instructions for the preprocessor.

Example:

<configuration>
<system.web>
 <!-- ifdef ${production} -->
 <!-- <compilation defaultLanguage="c#" debug="false"/> -->
 <!-- else -->
 <compilation defaultLanguage="c#" debug="true"/>
 <!-- endif -->
 </system.web>
</configuration>

Look carefully at the comments part of the above snippet. You can easily understand that we are trying to turn off the debug page compilation when in production. 

Now that is about the introduction, moving on to the actuals:

First thing first: Download the XMLPreproces from CodePlex, and extract the zip.

For this example, I will be using the spreadsheet(XML Spreadsheet 2003) as the storage of the environment specific variables. However you can also store it in a normal xml file.

I am using the sample files which came along with the download. I copied the sample.config and the SettingsSpreadSheet.xml into my xmpreprocess bin folder – I just did this to make the demo simpler – You can always provide the path for processing.

My folder looks like:

xmlprep1

Open the SettingsSpreadsheet.xml

xmlprep3

The columns represent the different environments you are targeting, and the values in the rows of Column A are the variables you will use in the config file. While preprocessing, the values of the variables are replaced with the corresponding value stored in the environment column.

For e.g. Let us say I want to generate a config file for testing environment, then I store the variable ServiceLocation in the column A, and then I provide the corresponding value in the column of “Test” – now when preprocessed “${ServiceLocation}” in the config file will be replaced with the corresponding value in the xml file.

Open the sample.config file;

xmlprep2

Look at the value in the comments section “${ServiceLocation}” – as mentioned above you should add this to the variable in the settings files – just add “ServiceLocation” (dollar and braces are not required in the settings file), and provide different values for different environments.

Now use this command to preprocess:

“XmlPreprocess.exe /v /q /nologo /i sample.config /x SettingsSpreadsheet.xml /e Test /o Processed.config /d test ”

Once you run the above command, it generates another config file named Processed.config and you will have the value ”testserver” instead of localhost, in the new config file.

Now think of adding this to your WIX setup, or calling the bat file with the preprocessing info as part of build scripts. This will reduce the work of maintaining different config files for different environments.

This tool is really simple to understand, and when used efficiently gets rid of maintenance problems.

Few other resources which may help:

Loren’s Blog

Scott Hanselman’s Blog

Cheers!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s