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