2012-06-14 10 views
193

Czy to możliwe, aby przekształcić następujący plik web.config appSettings:Jak zmienić wartość atrybutu w sekcji appSettings z transformacją Web.config

<appSettings> 
    <add key="developmentModeUserId" value="00297022" /> 
    <add key="developmentMode" value="true" /> 
    /* other settings here that should stay */ 
</appSettings> 

w coś takiego:

<appSettings> 
    <add key="developmentMode" value="false" /> 
    /* other settings here that should stay */ 
</appSettings> 

Tak więc muszę usunąć klucz developmentModeUserId i muszę zastąpić wartość klucza developmentMode.

Odpowiedz

345

Chcesz coś takiego:

<appSettings> 
    <add key="developmentModeUserId" xdt:Transform="Remove" xdt:Locator="Match(key)"/> 
    <add key="developmentMode" value="false" xdt:Transform="SetAttributes" 
      xdt:Locator="Match(key)"/> 
</appSettings> 

Zobacz http://msdn.microsoft.com/en-us/library/dd465326(VS.100).aspx aby uzyskać więcej informacji.

+20

Pamiętaj, że w klawisze są rozróżniane wielkie i małe litery! – Cosmin

+1

Doskonała odpowiedź. Próbowałem opcji innych firm, takich jak Wolny Gepard i nigdzie się nie dostałem - to było proste i doskonałe. – Steve

+2

@stevens: Potrzebowalibyście Powolnego Geparda, jeśli chcecie przekształcić, powiedzmy, plik app.config dla rodzimych aplikacji. Składnia powinna jednak być identyczna, jeśli sobie przypomnę (minęło trochę czasu, odkąd musiałem użyć Slow Cheetah). – Ellesedil

0

Wymiana wszystkich AppSettings

This is the overkill case where you just want to replace an entire section of the web.config. In this case I will replace all AppSettings in the web.config will new settings in web.release.config. This is my baseline web.config appSettings: 


<appSettings> 
    <add key="KeyA" value="ValA"/> 
    <add key="KeyB" value="ValB"/> 
</appSettings> 

Now in my web.release.config file, I am going to create a appSettings section except I will include the attribute xdt:Transform=”Replace” since I want to just replace the entire element. I did not have to use xdt:Locator because there is nothing to locate – I just want to wipe the slate clean and replace everything. 


<appSettings xdt:Transform="Replace"> 
    <add key="ProdKeyA" value="ProdValA"/> 
    <add key="ProdKeyB" value="ProdValB"/> 
    <add key="ProdKeyC" value="ProdValC"/> 
</appSettings> 



Note that in the web.release.config file my appSettings section has three keys instead of two, and the keys aren’t even the same. Now let’s look at the generated web.config file what happens when we publish: 


<appSettings> 
    <add key="ProdKeyA" value="ProdValA"/> 
    <add key="ProdKeyB" value="ProdValB"/> 
    <add key="ProdKeyC" value="ProdValC"/> 
</appSettings> 

Tak jak się spodziewaliśmy - AppSettings web.config zostały całkowicie zastąpione przez wartości w web.release config. To było łatwe!

Powiązane problemy