web.config transforms for <applicationSettings>
Asked Answered
S

5

16

I have web.config transforms for several environments. In the config file I have an applicationSettings section with several setting and value pairs.

I have tried based on the syntax I use to match name and change the connection strings to also match settings and change the value but the transforms are failing. Is this at all possible?

So my web.config has:

<applicationSettings>
    <AppName.My.MySettings>
        <setting name="setting1" serializeAs="String">
            <value>Initial Value</value>
        </setting>
    </AppName.My.MySettings>
</applicationSettings>

My transform file has

<applicationSettings>
    <add name="setting1" value="Changed Value" xdt:Transform="SetAttributes" xdt:Location="Match(name)"/>
</applicationSettings>

I get no errors when I preview the transform but whereas the connection string setting are transformed the value for setting1 is not. Any help appreciated.

UPDATE

<applicationSettings>
    <add name="setting1" value="Changed Value" xdt:Transform="Replace" xdt:Location="Match(name)"/>
</applicationSettings>

Unfortunately same problem... No errors and no transform.

SOLUTION I did forget to mention I have more than one setting so marked answer is partial solution... This is how I did it... Web.Config...

<applicationSettings>
    <AppName.My.MySettings>
        <setting name="setting1" serializeAs="String">
            <value>Initial Value 1</value>
        </setting>
        <setting name="setting2" serializeAs="String">
            <value>Initial Value 2</value>
        </setting>
        <setting name="setting3" serializeAs="String">
            <value>Initial Value 3</value>
        </setting>
    </AppName.My.MySettings>
</applicationSettings>

Transform File

<applicationSettings xdt:Transform="Replace">
    <AppName.My.MySettings>
        <setting name="setting1" serializeAs="String">
            <value>CHANGED VALUE 1</value>
        </setting>
        <setting name="setting2" serializeAs="String">
            <value>Initial value 2</value>
        </setting>
        <setting name="setting3" serializeAs="String">
            <value>CHANGED VALUE 3</value>
        </setting>
    </AppName.My.MySettings>
</applicationSettings>

Note I had to include all my nested settings and values even if some of them didn't change as in the case of setting 2 in my example.

Stargell answered 3/7, 2015 at 13:29 Comment(2)
value is a node not an attribute, and you have to use Transform="Replace"Hamitosemitic
@Andreas ... please see my update.. still no joy.Stargell
H
-4

original file:

<applicationSettings>
    <AppName.My.MySettings>
        <setting name="setting1" serializeAs="String">
            <value>Initial Value</value>
        </setting>
    </AppName.My.MySettings>
</applicationSettings>

Transform file:

 <applicationSettings>
   <AppName.My.MySettings>
      <setting name="setting1" serializeAs="String">
           <value xdt:Transform="Replace">Changed Value</value>
       </setting>
    </AppName.My.MySettings>
  </applicationSettings>
Hamitosemitic answered 3/7, 2015 at 13:55 Comment(6)
Andreas, transform on it's own did not work I had to use xdt:Transform="Replace" (case sensitive). I now have another problem. I have more than one setting (12 to be precise). The transform preview shows that the transform has been done but all against the first setting so at the end of the transform setting1 has the transformed value of setting12 while all the other setting2 to setting12 remain unchanged. Any ideas... I think I need a location.Stargell
have to see the xml to help youHamitosemitic
Sorted.... Instead of placing the xdt:Transform="Replace" within the value param I placed it with the root of applicationSettings and that worked... giving you credit as you pointed me in the right direction... updated question with correct solution.Stargell
As Mych mentioned, this will only replace the first setting value. So it won't work if you have 12 settings and only want to transform the 5th one.Ruphina
@CBauer As mentioned in my comment I placed the correct solution in my question.... See SOLUTION....Stargell
Yeah, I saw it, but you're not posting the answer in the answers section of the question, your posting the answer in the question portion of the question.Expanded
J
31

I know this is a little late, but the following transform file will allow you transform just one setting when you have multiple.

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <applicationSettings>
    <YourProject.Settings>
      <setting name="Log4NetPath" serializeAs="String" xdt:Transform="Replace" xdt:Locator="Match(name)">
        <value xdt:Transform="Replace">NewPath</value>
      </setting>
    </YourProject.Settings>
  </applicationSettings>
</configuration>
Jacquelinejacquelyn answered 14/9, 2016 at 15:15 Comment(0)
O
12

Transform file:

<applicationSettings>
   <AppName.My.MySettings>
      <setting xdt:Transform="Replace" xdt:Locator="Match(name)" name="setting1" serializeAs="String">
           <value>New Value</value>
       </setting>
    </AppName.My.MySettings>
  </applicationSettings>

Opheliaophelie answered 11/8, 2016 at 11:48 Comment(4)
Why isn't this one upvoted more? You don't need that extra xdt:Transform on <value>Nichol
Beware, this works only if you want replace one value. For two or more values does not work. Use solution from Dvirus1023.Philina
The "Dvirus1023" solution does not work. Do not replace the value.Opheliaophelie
Contrary to @KarelKral's experience this worked for me with three different settings in one file. Using Visual Studio 2019 and the latest version of transformMckellar
M
1

Agree with above answers. You need to determine if you replacing(transforming) a node or an attribute.

A node is value here: <value>Datasource=connection info</value>

An attribute is name here: <value name="connection info"/>

To replace a node, you use:

<value xdt:Transform="Replace">
Datasource="connection info";
</value>    

To replace an attribute:

<value name="other connection info"
   xdt:Transform="SetAttributes"
   xdt:Locator="Match(name)"/>

More detailed reference for replacing attributes: https://learn.microsoft.com/en-us/previous-versions/dd465326(v=vs.100)?redirectedfrom=MSDN

Manservant answered 25/6, 2021 at 18:33 Comment(0)
U
0

https://hk.saowen.com/a/7857cf5ec8f14af8504e7b9bb029e4cb0047336394e464a9b3807a9e1e587b93

Using Transform and Locator Attributes on Separate Elements

A Transform attribute does not have to be set in the same element as a Locator element. You can specify a Locator element on a parent element in order to select elements whose child elements you want to work with. You can then specify a Transform attribute in a child element in order to apply a change to the children.

The following example shows how to use the Locator attribute to select location elements for the specified path. However, only elements that are children of the selected location elements are transformed.

<configuration xmlns:xdt="...">
  <location path="C:\MySite\Admin" xdt:Locator="Match(path)"> 
    <system.web>
      <pages viewStateEncryptionMode="Always"
        xdt:Transform="SetAttributes(viewStateEncryptionMode)" />
    </system.web> 
  </location> 
</configuration>

If you specify a Locator attribute but you do not specify a Transform attribute in the same element or in a child element, no changes are made.

Uvula answered 3/1, 2019 at 16:22 Comment(0)
H
-4

original file:

<applicationSettings>
    <AppName.My.MySettings>
        <setting name="setting1" serializeAs="String">
            <value>Initial Value</value>
        </setting>
    </AppName.My.MySettings>
</applicationSettings>

Transform file:

 <applicationSettings>
   <AppName.My.MySettings>
      <setting name="setting1" serializeAs="String">
           <value xdt:Transform="Replace">Changed Value</value>
       </setting>
    </AppName.My.MySettings>
  </applicationSettings>
Hamitosemitic answered 3/7, 2015 at 13:55 Comment(6)
Andreas, transform on it's own did not work I had to use xdt:Transform="Replace" (case sensitive). I now have another problem. I have more than one setting (12 to be precise). The transform preview shows that the transform has been done but all against the first setting so at the end of the transform setting1 has the transformed value of setting12 while all the other setting2 to setting12 remain unchanged. Any ideas... I think I need a location.Stargell
have to see the xml to help youHamitosemitic
Sorted.... Instead of placing the xdt:Transform="Replace" within the value param I placed it with the root of applicationSettings and that worked... giving you credit as you pointed me in the right direction... updated question with correct solution.Stargell
As Mych mentioned, this will only replace the first setting value. So it won't work if you have 12 settings and only want to transform the 5th one.Ruphina
@CBauer As mentioned in my comment I placed the correct solution in my question.... See SOLUTION....Stargell
Yeah, I saw it, but you're not posting the answer in the answers section of the question, your posting the answer in the question portion of the question.Expanded

© 2022 - 2024 — McMap. All rights reserved.