【问题标题】:How to handle deep XDT transforms when structure of target config file is not known?当目标配置文件的结构未知时如何处理深度 XDT 转换?
【发布时间】:2013-08-15 13:11:51
【问题描述】:

我正在尝试使用 XDT 转换为我的 NuGet 包创建 web.config 安装程序。

我想转换 web.config 文件:

<configuration>
    <system.web>
    </system.web>
</configuration>

看起来像这样:

<configuration>
    <system.web>
        <httpHandlers>
            <add path="*." verb="*" type="CustomHandler" />
        </httpHandlers>
    </system.web>
</configuration>

这是我尝试过的转换:

变换 #1:

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <system.web>
        <httpHandlers>
            <add path="*." verb="*" type="CustomHandler" xdt:Transform="Insert" />
        </httpHandlers>
    </system.web>
</configuration>

这仅适用于目标 web.config 已包含 &lt;httpHandlers /&gt; 部分的情况。

在上面的示例中(注意,没有&lt;httpHandlers /&gt; 部分),这会导致错误:

源文档中没有元素匹配'/configuration/system.web/httpHandlers/add'

变换 #2:

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <system.web>
        <httpHandlers xdt:Transform="Insert">
            <add path="*." verb="*" type="CustomHandler" />
        </httpHandlers>
    </system.web>
</configuration>

这在上面的示例中按预期工作,但是给定一个带有预先存在的 &lt;httpHandlers /&gt; 部分的 web.config 文件,该部分是重复的。

请记住,这是针对 NuGet 包的,我无法对用户配置的状态做出假设。

我是 XDT 变换的新手,所以可能漏掉了一些明显的东西。

【问题讨论】:

    标签: xml nuget web-config-transform xdt-transform


    【解决方案1】:

    看起来像this will do the trick

    VS2012 中的新功能,xdt:Transform="InsertIfMissing"

    【讨论】:

    • 太棒了!谢谢,尼尔。
    • 可悲的是,没有对应的 xdt:Transform="RemoveIfEmpty" 来做相反的事情。
    • 嗯,他们确实开源了它......你可以自己写! xdt.codeplex.com
    猜你喜欢
    • 2014-01-08
    • 2011-11-26
    • 1970-01-01
    • 1970-01-01
    • 2013-08-01
    • 1970-01-01
    • 2013-04-11
    • 2018-02-23
    • 1970-01-01
    相关资源
    最近更新 更多