Click here to Skip to main content
12,255,210 members (39,265 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

14.3K views
Posted

Config Transformation Tool: Using XDT Transformation

, 29 Sep 2010 Ms-PL
Rate this:
Please Sign up or sign in to vote.
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

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.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)

Share

About the Author

outcoldman
Software Developer (Senior)
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionIs it possible to easily use the tool inside C# projects? Pin
rigame19-Apr-12 4:16
memberrigame19-Apr-12 4:16 
AnswerRe: Is it possible to easily use the tool inside C# projects? Pin
outcoldman19-Apr-12 7:20
memberoutcoldman19-Apr-12 7:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160426.1 | Last Updated 29 Sep 2010
Article Copyright 2010 by outcoldman
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid