Coder Perfect

How to use the Web.config transformation to modify the value of an attribute in the appSettings section?

Problem

Is it feasible to change the Web.config appSettings file as follows:

<appSettings>
    <add key="developmentModeUserId" value="00297022" />
    <add key="developmentMode" value="true" />
    /* other settings here that should stay */
</appSettings>

into something along these lines:

<appSettings>
    <add key="developmentMode" value="false" />
    /* other settings here that should stay */
</appSettings>

As a result, the key developmentModeUserId must be removed, and the value for the key developmentMode must be replaced.

Asked by dragonfly

Solution #1

You want something along the lines of:

<appSettings>
  <add key="developmentModeUserId" xdt:Transform="Remove" xdt:Locator="Match(key)"/>
  <add key="developmentMode" value="false" xdt:Transform="SetAttributes"
          xdt:Locator="Match(key)"/>
</appSettings>

Web.config Transformation Syntax for Web Application Project Deployment is also worth a look.

Answered by Joe

Solution #2

Replacing all AppSettings

When you only want to replace a piece of the web.config, this is the overkill scenario. In this scenario, I’ll replace all AppSettings in web.config with new web.release.config settings. My default web.config appSettings are as follows:

<appSettings>
  <add key="KeyA" value="ValA"/>
  <add key="KeyB" value="ValB"/>
</appSettings>

Now I’ll create an appSettings section in my web.release.config file, but this time I’ll include the property xdt:Transform=”Replace” because I only want to replace the complete element. I didn’t need to use xdt:Locator because there isn’t anything to find — I just want to start again and replace everything.

<appSettings xdt:Transform="Replace">
  <add key="ProdKeyA" value="ProdValA"/>
  <add key="ProdKeyB" value="ProdValB"/>
  <add key="ProdKeyC" value="ProdValC"/>
</appSettings>

My appSettings section in the web.release.config file has three keys instead of two, and the keys aren’t even the same. Let’s take a look at what occurs when we publish the produced web.config file:

<appSettings>
   <add key="ProdKeyA" value="ProdValA"/>
   <add key="ProdKeyB" value="ProdValB"/>
   <add key="ProdKeyC" value="ProdValC"/>
 </appSettings>

The web.config appSettings were totally replaced by the values in the web.release config, as intended. That was a breeze!

Answered by Joe

Solution #3

If you wish to change your app’s settings from a web config file to a web.Release.config file, follow the procedures below. Allow this to be your web.config app’s setting file:

<appSettings>
     <add key ="K1" value="Debendra Dash"/>
  </appSettings>

Here it is, the internet. For the transformation, use Release.config.

<appSettings>
    <add key="K1" value="value dynamicly from Realease"
       xdt:Transform="SetAttributes"
          xdt:Locator="Match(key)"   
         />
  </appSettings>

In realese Mode, this will change K1’s value to the new value.

Answered by Debendra Dash

Solution #4

I dislike conversions that contain more information than is required. So, rather than repeating the keys, I simply express the situation and objective. When done this way, it is much easier to see the intention, at least in my opinion. I also try to put all the xdt characteristics first to make it clear to the reader that these are transformations rather than new objects being defined.

<appSettings>
  <add xdt:Locator="Condition(@key='developmentModeUserId')" xdt:Transform="Remove" />
  <add xdt:Locator="Condition(@key='developmentMode')" xdt:Transform="SetAttributes"
       value="false"/>
</appSettings>

It is much easy to observe that the first one is eliminating the element in the example above. Setting characteristics is the second. Any properties you define here will be set/replaced. It will simply set value to false in this scenario.

Answered by CodingYoshi

Post is based on https://stackoverflow.com/questions/11033001/how-to-change-the-value-of-attribute-in-appsettings-section-with-web-config-tran