【问题标题】:Web.config transform moving namespace declarationWeb.config 转换移动命名空间声明
【发布时间】:2014-02-02 23:04:16
【问题描述】:

使用this online tester很容易看到以下问题

我的 web.config 看起来像:

<?xml version="1.0"?>
<configuration>
  <nlog/>
</configuration>

还有一个看起来像这样的变换:

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <xdt:Import assembly="AppHarbor.TransformTester" namespace="AppHarbor.TransformTester.Transforms"/>

  <nlog xdt:Transform="Replace"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <targets async="true">
      <target name="LogMill" xsi:type="FallbackGroup" returnToFirstOnSuccess="true">
        <target xsi:type="LogMillMessageBus"/>
        <target xsi:type="File" fileName="..\LogMill-FailSafe.log" layout="${TextErrorLayout}"/>
      </target>
    </targets>
  </nlog>
</configuration>

但输出不是我所期望的,它将 xsi 命名空间声明下移到使用它的元素,这导致 nlog 无法解析配置并出现错误Parameter p4 not supported on FallbackGroupTarget

<?xml version="1.0"?>
<configuration>
  <nlog>
    <targets async="true">
      <target name="LogMill" p4:type="FallbackGroup" returnToFirstOnSuccess="true" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">
      <target p4:type="LogMillMessageBus" /><target p4:type="File" fileName="..\LogMill-FailSafe.log" layout="${TextErrorLayout}" />
      </target>
    </targets>
  </nlog>
</configuration>

是否有我可以应用的转换选项或语法来防止它移动命名空间声明?我在the documentation 中找不到任何内容

【问题讨论】:

  • 它看起来像简单地删除 xmlns:xsi 并且只是在转换文件中使用 type="..." 而不是 xsi:type="..." 工作,但这会很好保留声明以改善 Visual Studio 中的编辑体验。
  • 这是一种解决方法(或者可能是解决方案?我不确定),但如果您在源配置&lt;nlog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/&gt; 中重新命名命名空间并在转换中使用&lt;nlog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xdt:Transform="Merge"&gt;,那么它就可以工作。
  • 我在 web.config 转换和 nlog 方面遇到了完全相同的问题。我通过删除转换规则中的命名空间避免了这个问题,因为在 Visual Studio 中工作时,智能感知只需要命名空间。在发布环境中没有必要。我意识到这已经有一年多了,但我认为我的评论可能会对某人有所帮助。

标签: c# xml web-config nlog


【解决方案1】:

将您的xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 声明移动到最顶部的元素,应该没问题

【讨论】:

    【解决方案2】:

    在我的情况下,而不是

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    

    我不得不使用

    xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"
    

    【讨论】:

      猜你喜欢
      • 2012-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-31
      • 1970-01-01
      • 2011-03-17
      相关资源
      最近更新 更多