I'm using web.config transformation as described in the below post in order to generate configs for different environments.
http://vishaljoshi.blogspot.com/2009/03/web-deployment-webconfig-transformation_23.html
I can do a "Replace" transformation by matching on the key, e.g.
<add key="Environment" value="Live" xdt:Transform="Replace" xdt:Locator="Match(key)" />
And I can do "Inserts" e.g.
<add key="UseLivePaymentService" value="true" xdt:Transform="Insert" />
But what I would really find useful is a ReplaceOrInsert transformation, as I can't always rely on the original config file having/not having a certain key.
Is there any way to do this?
The below creates a new key is the same key is not present. if present then it simply replaces the existing one.
<add key="some key" xdt:Transform="InsertIfMissing" xdt:Locator="Match(key)"/> <add key="some key" value="some value" xdt:Transform="Replace" xdt:Locator="Match(key)" />
In conjunction with
xdt:Transform="Remove"
usexdt:Transform="InsertIfMissing"
in VS2012.Use the
InsertIfMissing
transformation to ensure that the appSetting exists.Then use the
Replace
transformation to set its value.You could also use the
SetAttributes
transformation instead ofReplace
. The difference is thatSetAttributes
does not touch child nodes.These techniques are much better than remove+insert because existing nodes are not moved to the bottom of their parent node. New nodes are appended at the end. Existing nodes stay where they are in the source file.
This answer applies only to newer versions of Visual Studio (2012 or newer).
A better method for me was to insert the element only if it doesn't exist since I am only setting certain attributes. Removing the element would discard any other attributes of the main element if they existed.
example: web.config (without element)
web.config (with element)
Using the Locator with an XPath expression, I add the node if it doesn't exist and then set my attribute:
both resulting web.config files have includeExceptionDetailInFaults="true" and the second one preserves the httpsHelpPageEnabled attribute where the remove/insert method would not.
I found a cheap workaround. It ain't pretty and won't work very well if you have a lot of elements that needs to be "Replace Or Insert".
Do a "Remove" and then an "InsertAfter|InsertBefore".
For example,