如何在一个web.config元素上应用两个不同的转换?

15

我想在我的 VS2010 部署项目中对 web.config 中一个元素的两个不同属性应用两个不同的转换。请考虑以下 web.config 片段:

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

现在我如何在转换后的web.config中更改属性“attr1”并删除属性“attr5”?我知道如何执行单个转换:

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

并且:

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

但是我不知道如何将这些变换组合起来。有人可以帮忙吗?

编辑:

还不能回答自己的问题,但解决方案似乎是:

看起来可以通过以下方式重复使用同一个元素并应用不同的转换:

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

就像之前所说的,这似乎是有效的,但我不确定这是否是 web.config 转换语法的预期使用方式。


这是正确的,因为我一直在使用XmlTransforms。我相信你已经回答了这个问题 :) - Nick Nieslanik
2个回答

21

正如Nick Nieslanik所确认的那样,这是通过重复相同的元素并应用不同的转换来完成的,如下所示:

As Nick Nieslanik确认了这一点,它是通过重复相同的元素,并使用不同的变换来实现的,就像这样:

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

1
这个问题涉及将不同的转换应用于一个元素的不同属性,这是一个稍微不同的情况。 - Marco G

0
我正在使用XmlPreprocess工具进行配置文件的转换和操作。它只使用一个映射文件来适应多个环境。你可以通过Excel编辑映射文件,非常容易使用。 你可以使用xmlpreprocess更新你的配置文件,并将配置(debug、dev、prod等)作为不同设置的参数。

看起来这是一种非常强大的转换方式,但对于我目前的需求来说可能有些过头了。 - Marco G

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接