- | rssFeed | My book on MSBuild and Team Build | Archives and Categories Saturday, February 2, 2013

How to invoke XDT from code

A few weeks ago we released XDT on NuGet, XDT is the technology driving web.config transforms, on NuGet with a license allowing you to redistribute it with your applications. We are working to open source XDT as well so hopefully I will be able to announce that soon as well.

In this post I will show you how you can create a simple application which uses XDT. For this sample we will be creating a console application which will invoke XDT for you. In Visual Studio create a Console Application. I named this project XdtSample. After that we need to add the XDT library. We can easily do this using NuGet. You can execute the following command to insert the package into your project.

 Install-Package Microsoft.Web.Xdt -Pre

Note: after we release the final version you can remove the –Pre flag.

Executing this command will download the XDT assembly as well as add a reference to your project. In program.cs to have the following content.

namespace XdtSample {
    using Microsoft.Web.XmlTransform;
    using System;
    using System.IO;
    using System.Text;

    class Program {
        static void Main(string[] args) {
            if (args == null || args.Length < 3) {

            string sourceFile = args[0];
            string transformFile = args[1];
            string destFile = args[2];

            if (!File.Exists(sourceFile)) { throw new FileNotFoundException("sourceFile doesn't exist"); }
            if (!File.Exists(transformFile)) { throw new FileNotFoundException("transformFile doesn't exist"); }

            using (XmlTransformableDocument document = new XmlTransformableDocument()) {
                document.PreserveWhitespace = true;

                using (XmlTransformation transform = new XmlTransformation(transformFile)) {

                    var success = transform.Apply(document);


                        string.Format("\nSaved transformation at '{0}'\n\n",
                        new FileInfo(destFile).FullName));

                    int exitCode = (success == true) ? 0 : 1;

        static void ShowUsage() {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("\n\nIncorrect set of arguments");
            sb.AppendLine("\tXdtSample.exe sourceXmlFile transformFile destFile\n\n");

The files will be passed in as command line arguments. Below is the usage of the .exe.

xdtsample. sourceFile transformFile destFile 

In the main method here you can see that we load up the source file in an instance of XmlTransformableDocument. We use the XmlTransformation class to represent the transformFile. After applying the transform we simply save the result to the target location.

Pretty simple. Let me know if you have any questions.

I’ve posted this sample on github https://github.com/sayedihashimi/XdtSample.

Sayed Ibrahim Hashimi | http://msbuildbook.com | @SayedIHashimi

XDT Saturday, February 2, 2013 8:51:37 PM (GMT Standard Time, UTC+00:00)  #     | 
Wednesday, August 17, 2011

App.config transform Visual Studio Add in

When I talk to people about the XDT (web.config) transforms that web projects support for package/publish one of the most common questions is “Does this work for non-web projects?” Unfortunately the answer is No, but myself and a friend of mine (Chuck England) have created a Visual Studio add in which enables just this. You can find it in the Visual Studio gallery under the name SlowCheetah – XML Transforms. Here is an overview of the features of the add in.

  1. Add tooling to desktop project to create XDT transforms
  2. Transform app.config for desktop projects based on build configuration
  3. Transform any XML file to the output folder base on build configuration
  4. Add tooling to enable previewing XDT transforms
  5. For web projects easily transform other XML files during package/publish

After you install the add in you will get the following menu item when you right click any XML file.


For each build configuration defined you will get a transform created as a child item.


These files are stub XDT transforms. You can place your transform content in those files and they will be executed when you debug/run your application. For example if your app.config contained the following.

<?xml version="1.0" encoding="utf-8" ?>
    <add key="appName" value="WPF Demo-Debug-default"/>
    <add key="url" value="http://localhost:8080/Default/"/>
    <add key="email" value="demo-default@contoso.com"/>

    <clear />
    <add name="RecordsDb" connectionString=".\SQLExpress;Initial Catalog=RecordsDb-Default;Integrated Security=true"/>


And your app.debug.config contained

<?xml version="1.0" encoding="utf-8" ?>
<!-- For more information on using transformations 
     see the web.comfig examples at http://go.microsoft.com/fwlink/?LinkId=214134. -->
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">

    <add key="appName" value="WPF Demo-Debug" xdt:Transform="Replace" xdt:Locator="Match(key)"/>
    <add key="url" value="http://localhost:8080/" xdt:Transform="Replace" xdt:Locator="Match(key)"/>
    <add key="email" value="debug@contoso.com" xdt:Transform="Replace" xdt:Locator="Match(key)"/>

    <add name="RecordsDb" connectionString=".\SQLExpress;Initial Catalog=RecordsDb;Integrated Security=true"
         xdt:Transform="Replace" xdt:Locator="Match(name)"/>


When you run your application the config file created will contain the following.

<?xml version="1.0" encoding="utf-8"?>
    <add key="appName" value="WPF Demo-Debug"/>
    <add key="url" value="http://localhost:8080/"/>
    <add key="email" value="debug@contoso.com"/>

    <add name="RecordsDb" connectionString=".\SQLExpress;Initial Catalog=RecordsDb;Integrated Security=true"/>


Transforming other files

You are not limited to transforming app.config, you can transform any XML file. When you create a transform for an arbitrary XML file when you run your application the transformed XML file will be dropped into the output (bin) folder of your application.

Previewing Transforms

You can easily preview transform results as well. After you have created the transform file you can right click on it and select Preview Transform.


After you click it you should see the following.



I hope you guys find this useful. Let me know if you have any feedback on this!

Note: This add in was created by me and a friend of mine, these are not created nor supported by Microsoft.

Sayed Ibrahim Hashimi – @sayedihashimi

msbuild | Visual Studio 2010 | XDT Wednesday, August 17, 2011 6:25:37 AM (GMT Daylight Time, UTC+01:00)  #     |