Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Project Build: Web.config transformation

0.00/5 (No votes)
22 Aug 2012 1  
Web.Config Xml Transformation

Introduction

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.

Background

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

Web.Debug.Config
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <appSettings>
    <add key="ConnectionString" xdt:Locator="Match(key)" xdt:Transform="RemoveAll"/>
    <add key="ConnectionString" 
             value="SERVER=DEBUG_SERVER;UID=TESTUSER;PWD=TESTPASSWORD;" 
             xdt:Transform="Insert"/>
  </appSettings>
</configuration>
Web.Release.Config
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <appSettings>
    <add key="ConnectionString" xdt:Locator="Match(key)" xdt:Transform="RemoveAll"/>
    <add key="ConnectionString" 
            value="SERVER=PROD_SERVER;UID=PRODUSER;PWD=PRODPASSWORD;" 
            xdt:Transform="Insert"/>
  </appSettings>
  <system.web>
    <compilation xdt:Transform="RemoveAttributes(debug)">
    </compilation>
  </system.web>
</configuration>

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.

MyProject.csproj
<Project ToolsVersion="4.0" DefaultTargets="Build" 
              xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  ...
  <UsingTask TaskName="TransformXml" 
     AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll"/>
  <Target Name="ApplyTransform" Condition="Exists('Web.$(Configuration).config')">          
        <TransformXml Source="web.config" 
           Transform="web.$(Configuration).Config" Destination="web.config_output" />
  </Target>
  <Target Name="BeforeCompile">
	<CallTarget Targets="ApplyTransform"/>
  </Target>
  <Target Name="BeforeBuild">
	<Exec Command="attrib -r Web.config"/>
        <Copy SourceFiles="Web.config_output" DestinationFiles="Web.config" /> 
  </Target>
  ...
</Project>

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here