Web 配置转换:如果不存在则插入 [英] Web Config Transforms: Insert If Not Exists

查看:38
本文介绍了Web 配置转换:如果不存在则插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当且仅当目标中不存在匹配元素时,我才想应用转换.使用 http://webconfigtransformationtester.apphb.com/ 尝试各种 xpath 表达式,但到目前为止还没有运气.

例如如果目标 web.config 如下所示:

<预><代码><配置><system.web><编译调试=真"/></system.web></配置>

那么输出应该是这样的:

<预><代码><配置><连接字符串><add name="MyCs" provider="System.Data.SqlClient" connectionString=""/><add name="SomeOtherCs" provider="System.Data.SqlClient" connectionString=""/></connectionStrings><system.web><编译调试=真"/></system.web></配置>

但是如果目标看起来像这样:

<预><代码><配置><连接字符串><add name="MyCs" provider="System.Data.IChangedIt" connectionString="这里是我的连接字符串"/></connectionStrings><system.web><编译调试=真"/></system.web></配置>

那么转换的结果应该是这样的:

<预><代码><配置><连接字符串><add name="MyCs" provider="System.Data.IChangedIt" connectionString="这里是我的连接字符串"/><add name="SomeOtherCs" provider="System.Data.SqlClient" connectionString=""/></connectionStrings><system.web><编译调试=真"/></system.web></配置>

换句话说,我只是想将命名的连接字符串添加到配置中,但让管理员用他自己的值填充它.我认为它会像 xdt:Transform="Insert" xdt:Locator="XPath(count(/configuration/connectionStrings)=0)" 一样简单(如果不存在,则添加一个 cs 配置部分)但显然不是.

解决方案

在 VS2012 中将 xdt:Transform="InsertIfMissing"XmlTransform 任务一起使用.微软似乎还没有更新他们的文档来反映这一点.

I would like to apply a transformation if and only if a matched element does not exist in the target. Trying various xpath expressions using http://webconfigtransformationtester.apphb.com/ but no luck so far.

E.g. if the target web.config looks like this:

<configuration>
  <system.web>
    <compilation debug="true" />
  </system.web>
</configuration>

then the output should look like this:

<configuration>
  <connectionStrings>
    <add name="MyCs" provider="System.Data.SqlClient" connectionString="" />
    <add name="SomeOtherCs" provider="System.Data.SqlClient" connectionString="" />
  </connectionStrings>
  <system.web>
    <compilation debug="true" />
  </system.web>
</configuration>

But if the target looks like this:

<configuration>
  <connectionStrings>
    <add name="MyCs" provider="System.Data.IChangedIt" connectionString="my connection string here" />
  </connectionStrings>
  <system.web>
    <compilation debug="true" />
  </system.web>
</configuration>

then the result of the transformation should look like this:

<configuration>
  <connectionStrings>
    <add name="MyCs" provider="System.Data.IChangedIt" connectionString="my connection string here" />
    <add name="SomeOtherCs" provider="System.Data.SqlClient" connectionString="" />   
  </connectionStrings>
  <system.web>
    <compilation debug="true" />
  </system.web>
</configuration>

In other words, I just want to add the named connection string to configuration but let the administrator fill it in with his own values. I thought it would as simple as xdt:Transform="Insert" xdt:Locator="XPath(count(/configuration/connectionStrings)=0)" (to add a cs config section if none existed) but apparently not.

解决方案

Use xdt:Transform="InsertIfMissing" with the XmlTransform task in VS2012. It doesn't look like Microsoft has updated their documentation to reflect this yet.

这篇关于Web 配置转换:如果不存在则插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆