【问题标题】:Clearing out xdt element if it has no children如果没有子元素,则清除 xdt 元素
【发布时间】:2015-06-18 06:40:55
【问题描述】:

所以我开始使用 nuget,它是 web.config install/uninstall.xdt 值。

我的问题是,是否有 xdt:Transform 可以清除空元素。我在这里没有找到任何东西。 https://msdn.microsoft.com/en-us/library/dd465326%28v=vs.110%29.aspx

这是我的例子。

我当前的 Web.config.install.xdt 看起来像这样

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <system.web>
    <compilation xdt:Transform="InsertIfMissing">
      <assemblies xdt:Transform="InsertIfMissing">
        <add xdt:Transform="InsertIfMissing" xdt:Locator="Match(assembly)" assembly="MyAssembly, Version=4.5.4.0, Culture=neutral, PublicKeyToken=asdfasdfasdfasdf" />
      </assemblies>
    </compilation>
  </system.web>
</configuration>

我的卸载看起来像这样

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <system.web>
    <compilation>
      <assemblies>
        <add xdt:Transform="Remove" xdt:Locator="Match(assembly)" assembly="MyAssembly, Version=4.5.4.0, Culture=neutral, PublicKeyToken=asdfasdfasdfasdf" />
      </assemblies>
    </compilation>
  </system.web>
</configuration>

这是我之前的 web.config(简化)

  <system.web>
    <authentication mode="None" />
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>

这是我在 install.xtd 之后的 web.config

  <system.web>
    <authentication mode="None" />
    <compilation debug="true" targetFramework="4.5">
      <assemblies>
        <add assembly="MyAssembly, Version=4.5.4.0, Culture=neutral, PublicKeyToken=asdfasdfasdfasdf" />
      </assemblies>
    </compilation>
    <httpRuntime targetFramework="4.5" />
  </system.web>

这是我卸载后的 web.config

  <system.web>
    <authentication mode="None" />
    <compilation debug="true" targetFramework="4.5">
      <assemblies>
      </assemblies>
    </compilation>
    <httpRuntime targetFramework="4.5" />
  </system.web>

有没有办法去掉标签?

【问题讨论】:

    标签: c# asp.net nuget xdt-transform


    【解决方案1】:

    看来您可以在一个元素上指定多个转换。因此,您可以从安装中删除内容,然后检查该元素是否有子元素,如果没有则删除该元素。

    <?xml version="1.0"?>
    <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
      <system.web>
        <compilation>
          <assemblies>
            <add xdt:Transform="Remove" xdt:Locator="Match(assembly)" assembly="MyAssembly, Version=4.5.4.0, Culture=neutral, PublicKeyToken=asdfasdfasdfasdf" />
          </assemblies>
          <assemblies xdt:Locator="Condition(count(*) = 0)" xdt:Transform="Remove"/>
        </compilation>
      </system.web>
    </configuration>
    

    【讨论】:

    • 哇,谢谢,我会尽快测试您的解决方案。我将不得不重新修改我的代码,已经有一段时间了。
    • 谢谢,我今天早上试了一下,效果很好。很好的信息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-10
    • 1970-01-01
    • 2016-05-06
    • 2019-12-12
    • 2014-03-08
    相关资源
    最近更新 更多