Blog Home  Home Feed your aggregator (RSS 2.0)  
.Net Jonesie - Updating XML (config) files from a Team Build
A simple programmers blog
 
# Monday, November 21, 2005
The next step of my project deployment via Team Build is to update the exe.config file for the newly created web service.  To do this, I needed to create a task to edit an XML file.  Once again I looked at the SDC tasks, but found that it was easier to write my own.  I added the following to the build file.
<PropertyGroup>
   ...
  <TestDataUrl>http://blah/SUMSWS $(BuildNumber)/SUMSDataasmx</TestDataUrl>
  <TestSecurityUrl>http://blah/SUMSWS $(BuildNumber)/SUMSSecurity.asmx</TestSecurityUrl>
</PropertyGroup>
<UsingTask TaskName="AirwaysBuild.Tasks.XMLFile.ModifyXMLNode" AssemblyFile="Airways.Build.Tasks.dll"/>
<Target Name="AfterDropBuild">
  ... 
  <!-- update the app.config -->
  <ModifyXMLNode
    Filename="$(ClientDeployDest)\Airways.SUMS.exe.config"
    XPath="/configuration/applicationSettings/Airways.SUMS.Properties.Settings/setting[@name='WSDataURL']/value"
    NewValue="$(TestDataUrl)"
  />
  <ModifyXMLNode
    Filename="$(ClientDeployDest)\Airways.SUMS.exe.config"
    XPath="/configuration/applicationSettings/Airways.SUMS.Properties.Settings/setting[@name='WSSecurityURL']/value"
    NewValue="$(TestSecurityUrl)"
  />
  <ModifyXMLNode
    Filename="$(ClientDeployDest)\Airways.SUMS.exe.config"
    XPath="/configuration/applicationSettings/Airways.SUMS.Properties.Settings/setting[@name='BuildVersion']/value"
    NewValue="$(BuildNumber)"
  />

</Target>
 
The two new properties define the ASMX urls for the two services.  In the AfterDropBuild target I use these to update the exe.config file.  A third task updates a BuildVersion setting in the config which is displayed on the applications main window caption (so the user can clearly see which build they are running).
 
Each ModifyXMLNode task on the target defines the file name to modify, an XPath statement for the element or attribute and the new value to insert.  The new values are treated as text / strings and I use these in the task code thus:
XmlNode node = Document.DocumentElement.SelectSingleNode(XPath);
if (node != null)
{
  node.InnerText = NewValue;
}
I'm not sure if I should use InnerXml instead of InnerText - what I have now works well enough.
 
Once again, if you'd like the code for this task, shoot me an email or use the contact link on this blog or post a comment.  I'll probably be adding more tasks sooner or later.  When I get a bigger library of useful tasks I'll post it for download somewhere.
 
I must say that creating tasks for MSBuild is a lot of fun.  I'd be happy doing this all day every day.  You get to play with lots of different stuff, you don't have to create fancy UI and it's really very easy to do.  Back in NAnt days I never created a task - probably because there is already a vast library of free ones available, but also because I thought it was harder to do. 
Monday, November 21, 2005 9:53:52 AM (New Zealand Daylight Time, UTC+13:00)  #    Comments [1]   Team System  | 
Tuesday, November 22, 2005 8:36:41 AM (New Zealand Daylight Time, UTC+13:00)
Hee hee. You made my day :) That's a sweet little task, and what a nice comment about the fun you're having. We're glad you enjoy working with MSBuild!

Neil
Comments are closed.
Copyright © 2012 Peter G Jones. All rights reserved.
DasBlog 'Portal' theme by Johnny Hughes.
Pick a theme: