Web.config转换移动命名空间声明

时间:2014-02-02 23:04:16

标签: c# xml web-config nlog

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

中找不到任何内容

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

在我的情况下而不是

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

我不得不使用

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