如何转换web.config值?

时间:2013-04-22 09:12:37

标签: c# .net web-config app-config

我正在尝试为不同的情况(调试,发布等)制作不同的配置文件,我想更改不同版本的一些设置。

<configuration>
<applicationSettings>
<Program1.Properties.Settings>
  <setting name="CustomerId" serializeAs="String">
    <value>Custormer1-13256</value>
  </setting>
</Program1.Properties.Settings>
</applicationSettings>
</configuration>

如何将标签内的值更改为其他内容?

   ex. <value>Customer2-343242</value>

5 个答案:

答案 0 :(得分:13)

xdt:Locator="Match(name)"添加到Erwin's answer

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<applicationSettings>
    <Program1.Properties.Settings>
        <setting name="CustomerId" serializeAs="String" xdt:Transform="Replace" 
                                                             xdt:Locator="Match(name)">
            <value>Customer2-343242</value>
        </setting>
    </Program1.Properties.Settings>
</applicationSettings>

答案 1 :(得分:4)

将配置更改为:

<configuration>
  <applicationSettings>
    <Program1.Properties.Settings>
      <setting name="CustomerId" serializeAs="String">
        <value>Custormer1-13256</value>
      </setting>
    </Program1.Properties.Settings>

  </applicationSettings>
</configuration>

然后进行这样的转换:

<configuration>
  <applicationSettings>
    <Program1.Properties.Settings xdt:Transform="Replace">
      <setting name="CustomerId" serializeAs="String">
        <value>Custormer1-13256</value>
      </setting>
    </Program1.Properties.Settings>
  </applicationSettings>
</configuration>

所以你并没有真正“改变”内部文本的值,而是让它为你创建的每个构建配置选择正确的值。

答案 2 :(得分:3)

这有效:

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <applicationSettings>
        <Program1.Properties.Settings>
            <setting name="CustomerId" serializeAs="String" xdt:Locator="Match(name)" >
                <value xdt:Transform="Replace">Customer2-343242</value>
            </setting>
        </Program1.Properties.Settings>
    </applicationSettings>
</configuration>

答案 3 :(得分:0)

使用Web config transformations

<setting name="CustomerId" serializeAs="String" xdt:Transform="Replace">
  <value>Customer2-343242</value>
</setting>

答案 4 :(得分:0)

您需要将转换放在Web.Config XML的完整路径中

<applicationSettings>
  <Program1.Properties.Settings>
    <setting name="CustomerId">
      <value xdt:Transform="Replace">Customer2-343242</value>
    </setting>
  </Program1.Properties.Settings>
</applicationSettings>

然后使用“预览变换”菜单选项

对其进行测试