9

在 Visual Studio(web.config 转换)中,我有一个要执行的转换,它在根元素上添加两个属性。一种属性有效(但不是多种属性)。我可以在一个子元素上设置多个属性。我试过 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>

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

<configuration>

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

此转换旨在用于 element.config 文件(使用慢 cheetah)

更新 这显然也不起作用:

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

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

4

2 回答 2

16

您是否尝试过像这样通过将属性列表传递给 set to 来同时设置多个属性的文档转换SetAttribute()

请参阅此处形成更多信息。

具体来说: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>
于 2016-11-10T12:35:44.233 回答
0

The document element of a web.config file is <configuration>. In your example, <element> is probably a child of <configuration>. Try:

<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>
于 2012-07-31T08:29:40.937 回答