非web项目中使用XDT的实例(续)

简化

上个实例中,我们是引用了target文件来遍历项目中的xdt项。

当项目比较小,我们又只需要针对固定的config文件进行更替时,可以使用更简化的方法

不进行项目配置文件中的插入<Import Project="$(MSBuildExtensionsPath)\Custom\TransformFiles.targets" />

而直接写入代码

<UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v14.0\Web\Microsoft.Web.Publishing.Tasks.dll" />
  <Target Name="AfterCompile" Condition="Exists('app.$(Configuration).config')">
    <!--Generate transformed app config in the intermediate directory-->
    <TransformXml Source="app.config" Destination="$(IntermediateOutputPath)$(TargetFileName).config" Transform="app.$(Configuration).config" />
    <!--Force build process to use the transformed configuration file from now on.-->
    <ItemGroup>
      <AppConfigWithTargetPath Remove="app.config" />
      <AppConfigWithTargetPath Include="$(IntermediateOutputPath)$(TargetFileName).config">
        <TargetPath>$(TargetFileName).config</TargetPath>
      </AppConfigWithTargetPath>
    </ItemGroup>
  </Target>

项目运行时会自动替换Source="app.config"的文件为Transform="app.$(Configuration).config"

 

排错

当进行xdt时,最常见的几个问题

1. 在xml中xdt无法识别:

排错:请确认在xml头部引入了xmlns

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

 

2. 系统提示无法找到XXX.dll。

排错:首先请确认安装了MSbuild,其次请查看是否已经在配置中把版本号改成了正确的配置

 

3. 编译没有问题,但一运行就报错

排错:很可能是初始化的问题,请检查xml格式。 xml格式的要求很严格,尽量不要手写,有时候一个空格就会导致类似的问题。

转载于:https://www.cnblogs.com/cloversun/p/5767819.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值