Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply two different transformations on one web.config element?

From my VS2010 deployment project I would like to apply two different transformations to two different attributes of one element in my web.config. Consider the following web.config snippet:

<exampleElement attr1="false" attr2="false" attr3="true" attr4="~/" attr5="false">
  <supportedLanguages>
    <!-- Some more elements here -->
  </supportedLanguages>
</exampleElement>

Now how can I change attribute 'attr1' and remove attribute 'attr5' in the transformed web.config? I know how to perform the individual transformations:

<exampleElement attr1="true" xdt:Transform="SetAttributes(attr1)"></exampleElement>

and:

<exampleElement xdt:Transform="RemoveAttributes(attr5)"></exampleElement>

But I don't know how to combine these transforms. Anybody?

EDIT:

Can't answer my own question yet, but the solution seems to be:

It seems that it is possible to repeat the same element with different transformations, like so:

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <exampleElement attr1="true" xdt:Transform="SetAttributes(attr1)"></exampleElement>
    <exampleElement xdt:Transform="RemoveAttributes(attr5)"></exampleElement>
</configuration>

As said, this seems to work, but I'm not sure whether this is the intended use of the web.config transformation syntax.

like image 396
Marco G Avatar asked Jan 20 '12 14:01

Marco G


People also ask

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 Xdt transform in web config?

The xdt:Transform attribute value "SetAttributes" indicates that the purpose of this transform is to change attribute values of an existing element in the Web. config file.

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.


1 Answers

As Nick Nieslanik confirmed this is done by repeating the same element with different transformations, like so:

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <exampleElement attr1="true" xdt:Transform="SetAttributes(attr1)"></exampleElement>
    <exampleElement xdt:Transform="RemoveAttributes(attr5)"></exampleElement>
</configuration>
like image 73
Marco G Avatar answered Oct 07 '22 19:10

Marco G