Config Transformation Tool: Using XDT Transformation

XDT Transformation is a new feature of ASP.NET 4.0 named Web.Config Transformation.

Scott Guthrie: "In most real-world deployment scenarios, the web.config file you use for development is different than the one you use for production deployment. Typically you want to change environment settings like database connection-strings, making sure debug is turned off, and enabling custom errors so that end-users (and hackers) don’t see the internals of your application."

But the chief problem of this feature - is working only with web.config files.

I investigated this problem, and wrote Config Transformation Tool, which gives opportunity to use XDT Transformation Syntax like at Deployment Web Application Project for any files. This tool is very easy, it just run msbuild task, which do this transformation.

You just need to set source file, transformation file and destination file at arguments and run this tool. You can use it for app.config files for WinForms, WPF or Console projects and any other files. You can set this transformation task with nAnt or just set it in Post-Build Event for Project.

Below little example of using:

<?xml version="1.0"?>
 
<configuration>
 
    <custom>
        <groups>
            <group name="TestGroup1">
                <values>
                    <value key="Test1" value="True" />
                    <value key="Test2" value="600" />
                </values>
            </group>
 
            <group name="TestGroup2">
                <values>
                    <value key="Test3" value="True" />
                </values>
            </group>
 
        </groups>
    </custom>
    
</configuration>

transform.config file content:

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    
    <custom>
        <groups>
            <group name="TestGroup1">
                <values>
                    <value key="Test2" value="601" xdt:Transform="Replace"  xdt:Locator="Match(key)" />
                </values>
            </group>
        </groups>
    </custom>
    
</configuration>

Run tool from command line with arguments

ctt.exe s:source.config t:transform.config d:destination.config

Config Transformation Tool In Action

Tool will generate us destination.config file with content:

<?xml version="1.0"?>
 
<configuration>
 
    <custom>
        <groups>
            <group name="TestGroup1">
                <values>
                    <value key="Test1" value="True" />
                    <value key="Test2" value="601" />
                </values>
            </group>
 
            <group name="TestGroup2">
                <values>
                    <value key="Test3" value="True" />
                </values>
            </group>
 
        </groups>
    </custom>
    
</configuration>

To get more details about transform file syntax go to http://msdn.microsoft.com/en-us/library/dd465326.aspx

Project's URL at CodePlex http://ctt.codeplex.com/.

Current version Config Transformation Tool 1.0.3890.17440.

Comments (11)

Ivan Akcheurov ( ) #
gravatar
Hi,

It's greate to have some tool for modifying configs.

I've got a problem with XSLT which doesn't preserve the formatting of XML which is done by hand in configs mostly and readability is a sacred thing there.

Would your tool preserve the original formatting (indentation, newlines, so on) so that it's possible to keep the output identical to the input if needed?

Thanks for your efforts!
Denis Gladkikh ( ) #
gravatar
Hi Ivan,

I will take a look what I can do. Looks like this can be fixed with just one setting for XmlDocument: http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.preservewhitespace(v=vs.110).aspx, but I'm not sure if this is only one place.

Thanks,

Den
Ivan Akcheurov ( ) #
gravatar
Hi Denis,

I've tested XDT quite extensively.

It does a much better job than XSLT preserving the position of attributes, namespaces, etc.

MS has developed a whole bunch of special functionality for that (e.g. XmlAttributePreservingWriter from Microsoft.Web.Publishing.Tasks.dll).

Basically the parts of input that shouldn't be modified look almost identical to the corresponding output parts.

ALMOST is for newline characters in attributes :)

if you have the following:
<;SqlCommand query="SELECT *
                   FROM Table1">
then it gets to something like this:
<SqlCommand query="SELECT *&#xD;&#xA;                   FROM Table1">
if there is a semicolon in front of SqlCommand - that's an artifact of rendering

All of that happens because they use XmlTextWriter internally which uses XmlTextEncoder.Write method. The latter "entitifies" newlines if in attributes. It seems there is no option to tweak that and that's probably why MS guys didn't bother to much about it. The only way to fix this is probably to replace &#xa; back to \n carefully in the destination file afterwards.

Maybe you have a better solution to the problem?
Denis Gladkikh ( ) #
gravatar
One thing, which I can try to do is to do invoke HttpUtility.HtmlDecode for whole result, which should decode back these
&#xD;&#xA;
to the "new line".

But this can has side effect if you have these encoded characters in source.

One way is to add special argument to the ctt tool to do this after transformation. The second way is to try to encode all text values manually before transformation and restore them after, but I don't think that this will work always.

So, at first I will add the first way to doing this, and we will see, if this work for you - I will keep, if not - I will try to implement second way of doing this.
Denis Gladkikh ( ) #
gravatar
HttpUtility.HtmlDecode is not an option here, because then it will break this XML:
<value key="Test1" value="60
1&lt;group name=&quot;&quot;" />
I found that it is very often question about how to keep new lines in XmlDocument values. And looks like there are no good solution for this.
Denis Gladkikh ( ) #
gravatar
Hi Ivan,

I checked in and uploaded latest version with special argument "preservewhitespace" http://ctt.codeplex.com/releases/view/102388
Danil ( ) #
gravatar
Hi Denis,

could you point out briefly what are the differences between your tool and SlowCheetah: http://visualstudiogallery.msdn.microsoft.com/69023d00-a4f9-4a34-a6cd-7e854ba318b5
Denis Gladkikh ( ) #
gravatar
Hi Danil,

I wrote my tool when there were no alternatives. So I guess SlowCheetah can be just another XDT tool alternative. In my tool I also implemented some additional features, like parameters: Config Transformation Tool: Parameters support.
Jean-Pierre Fouche ( ) #
gravatar
1. I needed the following header in the xml files: 2. I needed to 'Save 'As' with Encoding in Visual Studio (ensuring Unicode UTF-8 is selected)

Otherwise the tool blew up (with the v for verbose parameter). Exception below:

c:\Temp\ctt>ctt.exe v s:Source.config t:Transform.config d:Dest.config

Start tranformation to 'Dest.config'.

Source file: 'Source.config'.

Transform file: 'Transform.config'.

Transformation task is using encoding 'System.Text.UnicodeEncoding'. Change encoding in source file if you want to change encoding.

Exception while transforming: System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1.

at System.Xml.XmlTextReaderImpl.Throw(String res, String arg)

at System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace()

at System.Xml.XmlTextReaderImpl.ParseDocumentContent()

at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace)

at System.Xml.XmlDocument.Load(XmlReader reader)

at Microsoft.Web.XmlTransform.XmlFileInfoDocument.Load(XmlReader reader)

at System.Xml.XmlDocument.LoadXml(String xml)

at Microsoft.Web.XmlTransform.XmlTransformation..ctor(String transform, Boolean isTransformAFile, IXmlTransformationLogger logger)

at OutcoldSolutions.ConfigTransformationTool.TransformationTask.Execute(String destinationFilePath, Boolean forceParametersTask).
Jeff Elliott ( ) #
gravatar
This is a fantastic tool. Do you have support for inserting comments in a file? For instance if I wanted to place a comment ahead of a config section I was entering?
Denis Gladkikh ( ) #
gravatar
Jeff, this tool is based on Microsoft XDT library, so this tool can do everything this library can do (plus also it supports parameters for inserts).
Submit Comment
If you want to get notifications about new comments at this topic, please fill email text box and check proper item. If you want to place source code in comment body place it in tags [code]...[/code], you can set language like this [code cs]...[/code], where cs can be cs, html, xml, java, js, php, sql, cpp, css.

 

busy