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

问题描述 投票:5回答:2

使用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找不到任何东西

c# xml web-config nlog
2个回答
1
投票

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


1
投票

在我的情况下而不是

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

我不得不使用

xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"
© www.soinside.com 2019 - 2024. All rights reserved.