Click here to Skip to main content
12,882,806 members (38,092 online)
Click here to Skip to main content
Add your own
alternative version


9 bookmarked
Posted 22 Aug 2012

Project Build: Web.config transformation

, 22 Aug 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
Web.Config Xml Transformation


We will learn about: replacing Web.config values using transformation during project build by using VS2010 TransformXml, transforms in debug or release mode from configuration files such as Web.Debug.Config and Web.Release.Config, preview transformation by using tools such as SlowCheetah. You can download SlowCheetah using the Extension Manager from VS2010.


Web.Config transformation syntax is located here, basically here is a simple sample for transformation. 

<configuration xmlns:xdt="">
    <add key="ConnectionString" xdt:Locator="Match(key)" xdt:Transform="RemoveAll"/>
    <add key="ConnectionString" 


<configuration xmlns:xdt="">
    <add key="ConnectionString" xdt:Locator="Match(key)" xdt:Transform="RemoveAll"/>
    <add key="ConnectionString" 


    <compilation xdt:Transform="RemoveAttributes(debug)">

Using the code

It is preferable to remove all existing values by using xdt:Locator="Match(key)" xdt:Transform="RemoveAll", and then xdt:Transform="Insert", this will ensure any existing values is removed and inserted with the new ones.

Edit the project file using Notepad, add the following tags to the project file. The tags: <UsingTask>, <BeforeCompile>, and <BeforeBuild> is to generate the transform file. The transformation assembly Microsoft.Web.Publishing.Tasks.dll does the actual xml transformation.

During the build process (Ctrl+Shift+B), the ApplyTransform task is executed before compile started, and this will create a output file "Web.Config_output", just before build process started, the output is renamed to "Web.Config", so that this will be the Web.Config used during runtime.

<Project ToolsVersion="4.0" DefaultTargets="Build" 

  <UsingTask TaskName="TransformXml" 

  <Target Name="ApplyTransform" Condition="Exists('Web.$(Configuration).config')">          
        <TransformXml Source="web.config" 

           Transform="web.$(Configuration).Config" Destination="web.config_output" />
  <Target Name="BeforeCompile">
	<CallTarget Targets="ApplyTransform"/>
  <Target Name="BeforeBuild">
	<Exec Command="attrib -r Web.config"/>
        <Copy SourceFiles="Web.config_output" DestinationFiles="Web.config" /> 

Points of Interest

Not just Web.Config can be transform, just about any XML file can be transformed, just add the TransfromXml element with the source and destination.

More articles about Web.Config transformation, click here.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Ken C. Len
Software Developer (Senior) LEN Associates Inc.
United States United States
Years of software consulting and software development using Microsoft development products such as Microsoft Content Management System, SQL Server Reporting Service, ASP.Net C# VB.Net, HTML and javascript web development, Visual Studio add-on development, C++ MFC/ATL and COM+ development, and ActiveX components.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
Dan Badea16-Apr-13 0:18
memberDan Badea16-Apr-13 0:18 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170422.1 | Last Updated 22 Aug 2012
Article Copyright 2012 by Ken C. Len
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid