2013-04-22 55 views
9

我想爲不同的情況(調試,發佈等)做不同的配置文件,我想改變一些設置爲不同的版本。如何轉換web.config值?

<configuration> 
<applicationSettings> 
<Program1.Properties.Settings> 
    <setting name="CustomerId" serializeAs="String"> 
    <value>Custormer1-13256</value> 
    </setting> 
</Program1.Properties.Settings> 
</applicationSettings> 
</configuration> 

如何將標籤內的值更改爲其他內容?

ex. <value>Customer2-343242</value> 
+0

你能解釋清楚一點?在web.config中有 – 2013-04-22 09:16:01

+0

我有以下代碼。在web.conf2.config中,我想將值更改爲customer2。我知道屬性可以通過xdt轉換,我想知道我們是否也可以將它應用於值。 – dare2k 2013-04-22 10:40:06

+0

我在找Erwin建議的東西。 – dare2k 2013-04-22 10:49:33

回答

0

使用Web config transformations

<setting name="CustomerId" serializeAs="String" xdt:Transform="Replace"> 
    <value>Customer2-343242</value> 
</setting> 
+0

我試圖在該標記中放入xdt:Transform =「Replace」,但不會更改任何值 – dare2k 2013-04-22 10:48:59

+0

您是否已通過右鍵單擊原始配置並選擇「添加配置轉換」來添加第二個配置? – Erwin 2013-04-22 10:59:01

+0

是的,我甚至試過它通過這個網站webconfigtransformationtester.apphb.com,並不取代任何 – dare2k 2013-04-22 11:03:34

0

你需要把完整路徑轉換爲你的web.config XML

<applicationSettings> 
    <Program1.Properties.Settings> 
    <setting name="CustomerId"> 
     <value xdt:Transform="Replace">Customer2-343242</value> 
    </setting> 
    </Program1.Properties.Settings> 
</applicationSettings> 

然後使用預覽變換菜單選項

測試
4

更改您的配置,如下所示:

<configuration> 
    <applicationSettings> 
    <Program1.Properties.Settings> 
     <setting name="CustomerId" serializeAs="String"> 
     <value>Custormer1-13256</value> 
     </setting> 
    </Program1.Properties.Settings> 

    </applicationSettings> 
</configuration> 

而做出變換是這樣的:

<configuration> 
    <applicationSettings> 
    <Program1.Properties.Settings xdt:Transform="Replace"> 
     <setting name="CustomerId" serializeAs="String"> 
     <value>Custormer1-13256</value> 
     </setting> 
    </Program1.Properties.Settings> 
    </applicationSettings> 
</configuration> 

那麼,你是不是真正的「改變」的值內文字這麼多,因爲有它選擇爲您創建的每個生成配置正確的價值。

+0

如果您要替換該部分中的所有內容,則此功能非常有用。 – 2017-03-07 00:30:38

12

添加xdt:Locator="Match(name)"Erwin's answer

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> 
<applicationSettings> 
    <Program1.Properties.Settings> 
     <setting name="CustomerId" serializeAs="String" xdt:Transform="Replace" 
                  xdt:Locator="Match(name)"> 
      <value>Customer2-343242</value> 
     </setting> 
    </Program1.Properties.Settings> 
</applicationSettings> 

2

這工作:

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> 
    <applicationSettings> 
     <Program1.Properties.Settings> 
      <setting name="CustomerId" serializeAs="String" xdt:Locator="Match(name)" > 
       <value xdt:Transform="Replace">Customer2-343242</value> 
      </setting> 
     </Program1.Properties.Settings> 
    </applicationSettings> 
</configuration>