Web配置转换:如果不存在则插入

Joã*_*nça 46 xpath web-config web-config-transform

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

例如,如果目标web.config看起来像这样:

<configuration>
  <system.web>
    <compilation debug="true" />
  </system.web>
</configuration>
Run Code Online (Sandbox Code Playgroud)

然后输出应该如下所示:

<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>
Run Code Online (Sandbox Code Playgroud)

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

<configuration>
  <connectionStrings>
    <add name="MyCs" provider="System.Data.IChangedIt" connectionString="my connection string here" />
  </connectionStrings>
  <system.web>
    <compilation debug="true" />
  </system.web>
</configuration>
Run Code Online (Sandbox Code Playgroud)

然后转换的结果应如下所示:

<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>
Run Code Online (Sandbox Code Playgroud)

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

小智 58

使用xdt:Transform="InsertIfMissing"XmlTransform在VS2012的任务.看起来微软已经更新了他们的文档以反映这一点.

  • 我正在使用VS 2012 - 但这对我不起作用:( (2认同)
  • 在VS2017中对我也不起作用;) (2认同)

Ger*_*leg 37

在我的情况下xdt:Transform="InsertIfMissing"没有工作xdt:Locator="Match(name)"


Kry*_*zal 12

xdt尝试此替代转换:Transform ="InsertIfMissing":

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <nodeToInsertIfMissing xdt:Transform="Insert" />
  <nodeToInsertIfMissing xdt:Transform="Remove" xdt:Locator="XPath(/configuration/nodeToInsertIfMissing[2])" />
</configuration>
Run Code Online (Sandbox Code Playgroud)

它应该遵循MSDN文档:

插入 - 将转换文件中定义的元素添加为所选元素的兄弟.新元素将添加到任何集合的末尾.

因此,如果节点已经存在,我们添加第二个节点,然后删除此节点(第二个).否则,我们添加新的唯一节点,但删除操作将失败.

注意:似乎无法使用NuGet *.(un)install.xdt转换.InsertIfMissing也是.


OzB*_*Bob 5

确认在VS2015和程序包管理器控制台主机版本3.4.4.1321中工作(您可以在打开程序包管理器控制台时找到它).

如果'configuration\connectionStrings\add\@name'不存在,则会插入.

app.config.install.xdt:

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <connectionStrings xdt:Transform="InsertIfMissing">
        <add name="MyCs" provider="System.Data.SqlClient" connectionString="" xdt:Transform="InsertIfMissing" xdt:Locator="Match(name)"/>
    </connectionStrings>
</configuration>
Run Code Online (Sandbox Code Playgroud)

.nu​​spec文件:

<files>
    <file src="app.config.install.xdt" target="content\app.config.install.xdt" />
Run Code Online (Sandbox Code Playgroud)