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

您好我在我的Nant构建脚本中使用Xpath来更改开发和我的其他环境之间的一些配置变量。 我从这个例子中采用了语法: 示例如下所示:
<xmlpoke
    file="config01/app.config"
    xpath="/configuration/appSettings/add[@key='AppName']/@value"
    value="TradeMonster">
</xmlpoke>
我想要的是类似于搜索我的连接字符串并查找“localhost SqlExpress”的所有实例,并将它们更改为“localhost” 这可能吗?     
已邀请:
在这里用一个快速的脏脚本玩弄.... 如果您确定每个文件中只有一个连接字符串元素,则可以使用
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>
    
XPath只选择节点,不能更改节点。 完成所需更改的一种方法是对XML文档执行XSLT转换。 为了实现这一点,您必须提供XML文档并准确指定要更改的文本节点。     

要回复问题请先登录注册