Search
Close this search box.

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>

posted on Tuesday, April 11, 2006 2:28 PM

This article is part of the GWB Archives. Original Author: paul’s

Related Posts