Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web Config Transforms: Insert If Not Exists

I would like to apply a transformation if and only if a matched element does not exist in the target. Trying various xpath expressions using http://webconfigtransformationtester.apphb.com/ but no luck so far.

E.g. if the target web.config looks like this:

<configuration>   <system.web>     <compilation debug="true" />   </system.web> </configuration> 

then the output should look like this:

<configuration>   <connectionStrings>     <add name="MyCs" provider="System.Data.SqlClient" connectionString="" />     <add name="SomeOtherCs" provider="System.Data.SqlClient" connectionString="" />   </connectionStrings>   <system.web>     <compilation debug="true" />   </system.web> </configuration> 

But if the target looks like this:

<configuration>   <connectionStrings>     <add name="MyCs" provider="System.Data.IChangedIt" connectionString="my connection string here" />   </connectionStrings>   <system.web>     <compilation debug="true" />   </system.web> </configuration> 

then the result of the transformation should look like this:

<configuration>   <connectionStrings>     <add name="MyCs" provider="System.Data.IChangedIt" connectionString="my connection string here" />     <add name="SomeOtherCs" provider="System.Data.SqlClient" connectionString="" />      </connectionStrings>   <system.web>     <compilation debug="true" />   </system.web> </configuration> 

In other words, I just want to add the named connection string to configuration but let the administrator fill it in with his own values. I thought it would as simple as xdt:Transform="Insert" xdt:Locator="XPath(count(/configuration/connectionStrings)=0)" (to add a cs config section if none existed) but apparently not.

like image 994
João Bragança Avatar asked Feb 27 '13 01:02

João Bragança


People also ask

What is Xdt transform in web config?

The xdt:Transform="RemoveAttributes(debug)" attribute specifies that you want the debug attribute to be removed from the system. web/compilation element in the deployed Web. config file. This will be done every time you deploy a Release build.

How do I create a new transformation in web config?

If you have a web application project, Right-click on web. config and choose Add Config Transform. This will add any config transforms that are missing from your project based on build configurations (i.e. if you have Production and Staging build configs, both will get a transform added).

What is Slowcheetah?

This package allows you to automatically transform your app. config (or any file) when you press F5 in Visual Studio. You can have different transformations based on the build configuration. This will enable you to easily have different app settings, connection strings, etc for Debug versus Release.

How do I rename a web config transform?

If you are using the extension Configuration Transform go to Build > Configuration Manager and find the Configuration dropdown for the project of which you want to change the app config. You can then select the edit and rename the build configurations for that project.


2 Answers

Use xdt:Transform="InsertIfMissing" with the XmlTransform task in VS2012. It doesn't look like Microsoft has updated their documentation to reflect this yet.

like image 85
ADW334034 Avatar answered Sep 19 '22 21:09

ADW334034


In my case xdt:Transform="InsertIfMissing" did not work without xdt:Locator="Match(name)"

like image 38
GerardBeckerleg Avatar answered Sep 18 '22 21:09

GerardBeckerleg