xml-transform

xml transforms in non-web projects using msbuild

Adding XML transforms to non-Web projects (Console, Winforms, WPF etc.)

  1. Add Debug/Release versions of confit as XDT transforms
  2. Add MSBuild.Microsoft.VisualStudio.Web.targets nuget PackageReference
  3. Import targets (conditionally or it won’t load the first time)
  4. Add TransformXml to AfterBuild
<Project>
    ...
      <ItemGroup>
        <None Include="App.config" />
        <None Include="App.Debug.config">
            <DependentUpon>App.config</DependentUpon>
            <SubType>Designer</SubType>
        </None>
        <None Include="App.Release.config">
            <DependentUpon>App.config</DependentUpon>
            <SubType>Designer</SubType>
        </None>
    </ItemGroup>
    ...
    <ItemGroup>
        <PackageReference Include="MSBuild.Microsoft.VisualStudio.Web.targets">
            <Version>14.0.0.3</Version>
        </PackageReference>
    </ItemGroup>
    ...
    <Import Condition="Exists('$(VSToolsPath)\Web\Microsoft.Web.Publishing.targets')" Project="$(VSToolsPath)\Web\Microsoft.Web.Publishing.targets" />
    ...
    <Target Name="AfterBuild">
        <TransformXml Source="@(AppConfigWithTargetPath)" Transform="App.$(Configuration).config" Destination="@(AppConfigWithTargetPath->'$(OutDir)%(TargetPath)')" />
    </Target>
</Project>