C# – How to change the value of attribute in appSettings section with Web.config transformation

asp.netcnetweb.config

Is it possible to transform the following Web.config appSettings file:

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

into something like this:

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

So, I need to remove the key developmentModeUserId, and I need to replace the value for the key developmentMode.

Best Answer

You want something like:

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

See Also: Web.config Transformation Syntax for Web Application Project Deployment