Set multiple attributes on root elemement using web.config transformation

Johan Leino picture Johan Leino · Jul 31, 2012 · Viewed 7.4k times · Source

In visual studio (web.config transformations) I have a transformation I want to perform which adds two attributes on the root element. One attrbute works (but not multiple ones). And I can set multiple attributes on a child element. I have tried SetAttributes with and without specifying the names of the attributes, no luck.

Ideas??

example

    <element xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform" xdt:Transform="SetAttributes" attrOne="One" attrTwo="Two">
      <children>
       <child name="One" xdt:Transform="SetAttributes" attrOne="One" attrTwo="Two" />
      </children>
    </element>

desired effect

    <element attrOne="One" attrTwo="Two">
      <children>
       <child name="One" attrOne="One" attrTwo="Two" />
      </children>
    </element>

The "element" section is really a custom section of the web.config file...like so:

<configuration>

... <element configSource="App_Data\element.config" />

this transformation is meant to be used on the element.config file (using slow cheetah)

Update This apparently doesn't work either:

<element xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform" xdt:Transform="Replace" attrOne="One" attrTwo="Two">
  <children>
   <child name="One" attrOne="One" attrTwo="Two" />
  </children>
</element>

But this does:

<element xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform" xdt:Transform="Replace" attrOne="One">
  <children>
   <child name="One" attrOne="One" attrTwo="Two" />
  </children>
</element>

As soon as there are more than 1 attribute on the root element it fails

Answer

chrisp_68 picture chrisp_68 · Nov 10, 2016

Have you tried a document transform like this that sets multiple attributes at the same time by passing a list of attributes to set to SetAttribute()?

See here form more info.

Specifically: Transform="SetAttributes(comma-delimited list of one or more attribute names)"

<element xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform" xdt:Transform="SetAttributes(attrOne,attrTwo)" attrOne="One" attrTwo="Two">
  <children>
   <child name="One" xdt:Transform="SetAttributes(attrOne,attrTwo)" attrOne="One" attrTwo="Two" />
  </children>
</element>