Using msbuild to change App.config and Web.config files

Using the msbuild community tasks project it becomes easy to change App.config and Web.config files as part of the build process. I've recently implemented a build script for a smart client application that allows building in multiple locations connecting to different application servers.

First, download the msbuild community tasks project. You can get this from subversion using the following command:

svn checkout http://msbuildtasks.tigris.org/svn/msbuildtasks/trunk msbuildtasks --username guest

Check the project out, build it and transfer the library and the .targets file to the desired directory. You can then import all of the tasks and start using the XmlUpdate task.

In order to work with App.config and Web.config you must add the appropriate namespace and reference the nodes in the xpath based on a custom-defined prefix. Here is a sample of this task in action: 

<Import Project=".\MSBuild.Community.Tasks.Targets"/>

<Target Name="UpdateConfigs" DependsOnTargets="GetVersion">
  <XmlUpdate
   Namespace="
http://schemas.microsoft.com/.NetConfiguration/v2.0"
   XmlFileName="$(SourceDir)\Core\ABSuite\ABClient\App.config"
   Xpath="//configuration/appSettings/add[@key='Main.ConnectionString']/@value"
   Value="$(DatabaseConnectionString)"
  />
  <XmlUpdate
   Namespace="
http://schemas.microsoft.com/.NetConfiguration/v2.0"
   XmlFileName="$(SourceDir)\Core\ABSuite\ABClient\App.config"
   Xpath="//configuration/appSettings/add[@key='Main.WebServer']/@value"
   Value="$(AppServiceServer)$(AppServiceVirtualDir)/"
  />
  <XmlUpdate
   Namespace="
http://schemas.microsoft.com/.NetConfiguration/v2.0"
   XmlFileName="$(SourceDir)\Core\ABSuite\GenesisAppServer\WebService\Web.config"
   Xpath="//configuration/appSettings/add[@key='Main.ConnectionString']/@value"
   Value="$(DatabaseConnectionString)"
  />
  <XmlUpdate
   Namespace="
http://schemas.microsoft.com/.NetConfiguration/v2.0"
   XmlFileName="$(SourceDir)\Core\ABSuite\GenesisAppServer\WebService\Web.config"
   Xpath="//configuration/appSettings/add[@key='CatalogNameToUse']/@value"
   Value="$(DatabaseName)"
  />
  <XmlUpdate
   Prefix="n"
   Namespace="
http://schemas.microsoft.com/.NetConfiguration/v2.0"
   XmlFileName="$(SourceDir)\Core\ABSuite\GenesisAppServer\WebService\Web.config"
   Xpath="//n:configuration/n:appSettings/n:add[@key='Server.Version']/@value"
   Value="$(Major).$(Minor).$(Build).$(Revision)"
  />
  <XmlUpdate
   Prefix="n"
   Namespace="
http://schemas.microsoft.com/developer/msbuild/2003"
   XmlFileName="$(SourceDir)\Core\ABSuite\ABClient\ABClient.csproj"
   Xpath="/n:Project/n:PropertyGroup/n:PublishUrl"
   Value="$(ClickOnceServer)$(ClickOnceVirtualDir)/"
  />
  <XmlUpdate
   Prefix="n"
   Namespace="
http://schemas.microsoft.com/developer/msbuild/2003"
   XmlFileName="$(SourceDir)\Core\ABSuite\ABClient\ABClient.csproj"
   Xpath="/n:Project/n:PropertyGroup/n:BootstrapperComponentsUrl"
   Value="$(ClickOnceServer)$(ClickOnceVirtualDir)/"
  />
  <XmlUpdate
   Prefix="n"
   Namespace="
http://schemas.microsoft.com/developer/msbuild/2003"
   XmlFileName="$(SourceDir)\Core\ABSuite\ABClient\ABClient.csproj"
   Xpath="/n:Project/n:PropertyGroup/n:ApplicationVersion"
   Value="$(Major).$(Minor).$(Build).$(Revision)"
  />
 </Target>

 

 

 

 

Print | posted @ Tuesday, April 11, 2006 2:28 PM

Comments on this entry:

Gravatar # VSTS Links - 04/12/2006
by Team System News at 4/12/2006 6:53 AM

Anutthara talks about using Team Build with VSS or your own source control.

Paul Whitaker blogs about...
Gravatar # re: Using msbuild to change App.config and Web.config files
by Ali at 6/22/2006 4:51 PM

It would be helpful to view the entire proj file...is there anyway you can do that?
Gravatar # re: Using msbuild to change App.config and Web.config files
by Gordon at 10/5/2006 7:36 AM

This has been very useful. I can now amend keys but can I add keys?
Gravatar # SolutionToBuild ahs invalid element for namespace in http://schemas.microsoft.com/developer/msbuild/2003
by Rifaqat Ali Siddiqui at 9/11/2007 8:41 PM

hi,
SolutionToBuild has invalid element for namespace in http://schemas.microsoft.com/developer/msbuild/2003,what configuration i need to resolve to this issue for team Foundation.
Gravatar # re: Using msbuild to change App.config and Web.config files
by Martyn at 10/5/2007 2:57 AM

Hi,
My web.config root element is: <Configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0>

The program only works for me if I remove this xmlns reference, leaving just <configuration> as the root. How can I resoilve this?
Gravatar # re: Using msbuild to change App.config and Web.config files
by Robz at 7/2/2008 5:34 PM

Remember that XMLUpdate's XPath is case sensitive. I love not having 5+ config files to maintain. It is awesome

Your comment:

Title:
Name:
Email:
Website:
 
Italic Underline Blockquote Hyperlink
 
 
Please add 2 and 7 and type the answer here: