Clearing out xdt element if it has no children

荒凉一梦 提交于 2019-12-10 13:19:59

问题


So I'm starting to play with nuget and it's web.config install/uninstall.xdt values.

My question would be, Is there and xdt:Transform that will clear out empty elements. I didn't find anything here. https://msdn.microsoft.com/en-us/library/dd465326%28v=vs.110%29.aspx

Here's my example.

My current Web.config.install.xdt looks like this

<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>

and my uninstall looks like this

<?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>

Here's my web.config before (simplified)

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

Here's my web.config after the install.xtd

  <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>

Here's my web.config after the uninstall

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

Is there anyway to get rid of the tag?


回答1:


It appears you can specify multiple transformations on an element. Therefore you could remove the content from your install and then check if the element has children, if not remove the element.

<?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>


来源:https://stackoverflow.com/questions/28592158/clearing-out-xdt-element-if-it-has-no-children

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!