Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How best to update an XML node in MSBuild

I have been using the Tigris community tasks to update various AppSettings keys using the XMLUpdate task.

Now, however I want to add a node to the system.net section to set up the proxy.

I declared a property

<PropertyGroup>
    <proxy>&lt;defaultProxy&gt; &lt;proxy usesystemdefault="False" proxyaddress="http://IPADDRESS:PORT" /&gt; &lt;/defaultProxy&gt;</proxy>
  </PropertyGroup>

and the XMLUpdate Task looks like

<XmlUpdate
 Prefix="n"
 Namespace="http://schemas.microsoft.com/.NetConfiguration/v2.0"
 XmlFileName="$(BuildDir)\Builds\_PublishedWebsites\Presentation\Web.config"
 XPath="/n:configuration/n:system.net"
 Value="$(proxy)"  />

this updates the web config however it updates directly from the property group i.e. doesn't convert the escape characters for the angle brackets. Does anyone have any ideas?

like image 236
Dean Avatar asked Feb 02 '09 16:02

Dean


1 Answers

You could use the XmlMassUpdate instead of XmlUpdate task.

<ProjectExtensions>
  <defaultProxy>
    <proxy usesystemdefault="False" proxyaddress="http://IPADDRESS:PORT"/>
  </defaultProxy>
</ProjectExtensions>

<Target Name="SubstituteFromWebConfig">
  <XmlMassUpdate 
    NamespaceDefinitions="msb=http://schemas.microsoft.com/developer/msbuild/2003;n=http://schemas.microsoft.com/.NetConfiguration/v2.0"
    ContentFile="$(BuildDir)\Builds\_PublishedWebsites\Presentation\Web.config" 
    ContentRoot="/n:configuration/n:system.net"
    SubstitutionsFile="$(MSBuildProjectFullPath)"
    SubstitutionsRoot="/msb:Project/msb:ProjectExtensions/msb:system.web" />
</Target> 

In this example we replace the node pointed by ContentRoot in ContentFile by the one pointed by SubstitutionsRoot in SubstitutionsFile (The current MSBuild file).

This technique takes advantage of the MSBuild ProjectExtensions element which allows you to add XML to a project file that will be ignored by the MSBuild engine.

(Or if you do not want to use XmlMassUpdate, you could use the XmlRead task on a node in ProjectExtensions and a XmlUpdate.)

like image 93
Julien Hoarau Avatar answered Sep 28 '22 03:09

Julien Hoarau