How to invoke XDT from code

Comments [0]

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) {
                ShowUsage();
                return;
            }

            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;
                document.Load(sourceFile);

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

                    var success = transform.Apply(document);

                    document.Save(destFile);

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

                    int exitCode = (success == true) ? 0 : 1;
                    Environment.Exit(exitCode);
                }
            }
        }

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

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


Comment Section

Comments are closed.