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