2010-05-05 16 views
5

嗨我在我的Nant構建腳本中使用Xpath來更改開發和我的其他環境之間的一些配置變量。xmlpoke in Nant - 如何更新找到的字符串的所有實例

我已經採取了語法,從這個例子:

的例子是這樣的:

<xmlpoke 
    file="config01/app.config" 
    xpath="/configuration/appSettings/add[@key='AppName']/@value" 
    value="TradeMonster"> 
</xmlpoke> 

我想什麼是類似於此搜索我的連接字符串和發現的所有實例的東西「 localhost \ SqlExpress「,只是將它們更改爲」localhost「

這可能嗎?

回答

4

與這裏quick'n髒腳本玩弄....

如果您確信只有一個在每個ConnectionString的元素文件,您可以用xmlpeekxmlpoke組合實現這一點。修改字符串更容易一些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> 
+0

感謝彼得 - 偉大的解決方案! – Remotec 2010-05-10 10:31:19

+0

這真的很有幫助。 – 2014-09-24 00:50:36

0

XPath只選擇節點,它不能更改節點

完成所需更改的一種方法是對XML文檔執行XSLT轉換。

爲了做到這一點,您必須提供XML文檔並準確指定要更改哪些文本節點。

+0

的XPath只選擇節點,但xmlpoke(南特的功能)的變化與XPath選擇的節點。 Xmlpoke就是問題所在。 – azheglov 2011-10-21 16:23:42

+0

@azheglov:這個問題被標記爲「xpath」。即使是「我正在使用XPath更改...」也是不正確的。 OP希望XPath表達式「搜索我的連接字符串」,但他沒有提供要應用XPath表達式的XML文檔 - 因此這是一個不完整的問題。這是我在答覆中所表達的一切。 – 2011-10-21 17:03:26

相關問題