by Oliver
11. May 2016 12:24
How do I modify an entry in the appSettings node during deployment?
We use a custom MSBuild script to deploy our discoverize portals and wanted to set a portal dependent key, namely NewRelic.AppName. So how did we do it? We use the XmlUpdate task from the MSBuild Community Tasks project. Here's the code:
- <!-- Import all MSBuild Community Tasks -->
- <Import Project="$(LibFolder)\msbuild\MSBuild.Community.Tasks.Targets" />
-
- <!-- Define the property value we want to use below -->
- <PropertyGroup>
- <NewRelicAppName>$([System.IO.Path]::GetFileName($(Destination))), Discoverize Portals</NewRelicAppName>
- </PropertyGroup>
-
- <!-- Update NewRelic.AppName key in appSettings of web.config -->
- <XmlUpdate XmlFileName="$(Destination)\web.config"
- XPath="/configuration/appSettings/add[@key='NewRelic.AppName']/@value"
- Value="$(NewRelicAppName)" />
I'm no XPath guru, so I was glad to find this thread that contained the XPath expression needed to select a certain appSettings entry by key and update its value.
If you seek to delete an element from an XML file, this stackoverflow answer taught me that there is also a Delete attribute on the XmlUpdate task :-)
Happy deploying!