C#应用程序app.config转换无法从父级到子级

时间:2018-09-03 09:20:38

标签: c# asp.net .net asp.net-core console-application

我正在尝试对服务app.config中的子元素应用转换。 我有几种服务,只需要替换一个属性即可。

示例服务条目

<configuration>
     <cronService>
            <cronSettings>
                <services>
                    <service name="Name1" assembly="xxx.yyy.Applications.dll" interval="300" wakeUpTime="" onErrorEmail="developmentteam@zzz.com" continueAfterError="true" notifyEmailOnError="true" runOnStart="true" enable="true" />
    <service name="Name2" assembly="xxx.lll.Applications.dll" interval="300" wakeUpTime="" onErrorEmail="developmentteam@zzz.com" continueAfterError="true" notifyEmailOnError="true" runOnStart="true" enable="true" />
                </services>
            </cronSettings>
        </cronService>
</configuration>

在转换文件中,我尝试在父级应用xdt:Transform,并希望子级得到更改

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <cronService>
        <cronSettings emailSender="{AppTierCloudService}@zzz.com" xdt:Transform="SetAttributes" >
        <services xdt:Transform="SetAttributes" >
            <service onErrorEmail="developmentteam@www.com, abb@www.com"/>
        </services>
        </cronSettings>
    </cronService>
</configuration>

引用了此https://msdn.microsoft.com/en-us/library/dd465326.aspx

但是这种转变没有发生。有帮助吗?

1 个答案:

答案 0 :(得分:1)

在子作品中添加xdt:Transform="SetAttributes"

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <cronService>
        <cronSettings emailSender="{AppTierCloudService}@zzz.com" xdt:Transform="SetAttributes">
            <services>
                <service onErrorEmail="developmentteam@www.com, abb@www.com" xdt:Transform="SetAttributes" />
            </services>
        </cronSettings>
    </cronService>
</configuration>

转换后的XML如下所示:

<configuration>
    <cronService>
        <cronSettings emailSender="{AppTierCloudService}@zzz.com">
            <services>
                <service name="Name1" assembly="xxx.yyy.Applications.dll" interval="300" wakeUpTime="" onErrorEmail="developmentteam@www.com, abb@www.com" continueAfterError="true" notifyEmailOnError="true" runOnStart="true" enable="true" />
                <service name="Name2" assembly="xxx.lll.Applications.dll" interval="300" wakeUpTime="" onErrorEmail="developmentteam@www.com, abb@www.com" continueAfterError="true" notifyEmailOnError="true" runOnStart="true" enable="true" />
            </services>
        </cronSettings>
    </cronService>
</configuration>

特别感谢@Kirk Larkin显示了此即时变换检查器工具:https://webconfigtransformationtester.apphb.com