Nant中的xmlpoke - 如何更新找到的字符串的所有实例

Rem*_*tec 5 nant xpath xmlpoke

您好我在我的Nant构建脚本中使用Xpath来更改开发和我的其他环境之间的一些配置变量.

我从这个例子中采用了语法:

示例如下所示:

<xmlpoke
    file="config01/app.config"
    xpath="/configuration/appSettings/add[@key='AppName']/@value"
    value="TradeMonster">
</xmlpoke>
Run Code Online (Sandbox Code Playgroud)

我想要的是类似于搜索我的连接字符串并查找"localhost\SqlExpress"的所有实例,并将它们更改为"localhost"

这可能吗?

Pet*_*old 4

在这里玩弄一个又快又脏的脚本......

如果您确定每个文件中只有一个连接字符串元素,您可以通过xmlpeek和的组合来完成此操作xmlpoke。使用某些 C# 可以更轻松地修改字符串,因此使用脚本任务来执行正则表达式搜索和替换:

 <script language="C#" prefix="custom" >
      <code>
        <![CDATA[
          [Function("fix")]
          public static string Fix(string input) {
              return Regex.Replace(input, @"localhost\\\w+", "localhost");
          }
        ]]>
      </code>
  </script>

<!-- Get the existing connection string -->
<xmlpeek
    file="config01/app.config"
    xpath="/configuration/connectionStrings/add[@contains(@connectionString,'localhost\')]/@connectionString"
    property="connectionstring">
</xmlpeek>

<!-- Write back the modified connection string -->
<xmlpoke
    file="config01/app.config"
    xpath="/configuration/connectionStrings/add[@contains(@connectionString,'localhost\')]/@connectionString"
    value="${custom::fix(connectionstring)}">
</xmlpoke>
Run Code Online (Sandbox Code Playgroud)