[英]Transform app.config entries only if a flag in app.debug.config is true
我想在Debug
配置中執行xdt:Transform
,但前提是app.debug.config
中的條目值是某個值,讓我們說true
以保持簡單。 例如:
應用程序配置:
<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings>
<add key="Value.First" value="foo" />
<add key="Value.Second" value="foo" />
<add key="Value.Third" value="foo" />
</appSettings>
</configuration>
應用程序調試配置:
<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings>
<!--Convenient flag to indicate if transform should happen-->
<add key="Perform.Transform.If.True" value="true" xdt:Transform="Insert" />
<!--Should only happen if the above is true-->
<add key="Value.First" value="bar" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
<add key="Value.Second" value="bar" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
<add key="Value.Third" value="bar" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
</appSettings>
</configuration>
我希望只有在鍵Perform.Transform.If.True
設置為true
時才轉換 app.config 中的所有Value.*
條目。 如果它是false
的,什么都不應該發生。 原因是有時在測試期間我們想快速打開和關閉由配置文件控制的東西。
我已經看到了Locator
的選項,例如 Match、Conditional、XPath 等,但似乎沒有一個允許來自另一個條目的條件。 可以用 slowcheetah/xdt 轉換來完成嗎?
好吧,找到了一種使用XPath
的迂回方式:
<add
key="Value.First"
value="bar"
xdt:Transform="Replace"
xdt:Locator="XPath(//appSettings/add[@key='Perform.Transform.If.True' and translate(@value, 'ERTU', 'ertu')='true']/../add[@key='Value.First'])"
/>
如果標志不是true
,它將無法解析路徑。 translate
使其不區分大小寫。
無法解決(是false
)會導致編譯警告,這很煩人,但由於這是一個調試工具,我們可以忍受。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.