使用web.config转换在根元素上设置多个属性

问题描述 投票:6回答:2

在visual studio(web.config转换)中,我有一个我想要执行的转换,它在根元素上添加了两个属性。一个attrbute工作(但不是多个)。我可以在子元素上设置多个属性。我已尝试使用和不指定属性名称的SetAttributes,没有运气。

想法?

    <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>

期望的效果

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

“element”部分实际上是web.config文件的自定义部分......就像这样:

<configuration>

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

这个转换意味着在element.config文件中使用(使用慢速猎豹)

更新这显然不起作用:

<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>

但这样做:

<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>

只要根元素上有多个属性,它就会失败

asp.net .net web-config web-deployment web-config-transform
2个回答
9
投票

您是否尝试过这样的文档转换,通过将属性列表设置为SetAttribute()来同时设置多个属性?

请参阅here表格了解更多信息。

具体来说:Transform =“SetAttributes(一个或多个属性名称的逗号分隔列表)”

<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>

0
投票

web.config文件的文档元素是<configuration>。在你的例子中,<element>可能是<configuration>的孩子。尝试:

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <element xdt:Transform="SetAttributes" attrOne="One" attrTwo="Two">
        <children>
            <child xdt:Transform="SetAttributes" name="One"
                   attrOne="One" attrTwo="Two" />
        </children>
    </element>
</configuration>
© www.soinside.com 2019 - 2024. All rights reserved.