Geeks With Blogs
Paul's development blog Tidbits from Smart Client, Windows Forms, C#, .NET 2.0, XML, and related toys...

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 | Back to top


Comments on this post: Using msbuild to change App.config and Web.config files

# VSTS Links - 04/12/2006
Requesting Gravatar...
Anutthara talks about using Team Build with VSS or your own source control.

Paul Whitaker blogs about...
Left by Team System News on Apr 12, 2006 6:53 AM

# re: Using msbuild to change App.config and Web.config files
Requesting Gravatar...
It would be helpful to view the entire proj file...is there anyway you can do that?
Left by Ali on Jun 22, 2006 4:51 PM

# re: Using msbuild to change App.config and Web.config files
Requesting Gravatar...
This has been very useful. I can now amend keys but can I add keys?
Left by Gordon on Oct 05, 2006 7:36 AM

# re: Using msbuild to change App.config and Web.config files
Requesting Gravatar...
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?
Left by Martyn on Oct 05, 2007 2:57 AM

# re: Using msbuild to change App.config and Web.config files
Requesting Gravatar...
Remember that XMLUpdate's XPath is case sensitive. I love not having 5+ config files to maintain. It is awesome
Left by Robz on Jul 02, 2008 5:34 PM

# re: Using msbuild to change App.config and Web.config files
Requesting Gravatar...
When I use XMLUpdate, I get an error that access to path is denied. How can I remove read only setting from Folder/File using msbuild?
Left by Disha on Feb 12, 2009 12:39 AM

# re: Using msbuild to change App.config and Web.config files
Requesting Gravatar...
I used the following XPath
XPath="//configuration/connectionStrings/add[@key=connectionString]/@value"

It has no errors but it updated nothing...
It give a message as
"0 node(s) selected for update."

How can I get rid of this problem...?
Left by Pradeep on Feb 22, 2011 11:01 PM

Your comment:
 (will show your gravatar)
 


Copyright © Paul Whitaker | Powered by: GeeksWithBlogs.net | Join free