如何在xml文件上强制配置转换?

时间:2017-10-27 03:49:53

标签: c# .net visual-studio-2017

我刚为VS2017安装了Configuration Transform,希望转换一些XML文件。

不幸的是,它看起来只有* .config文件?!

例如,当我点击* .config文件时,我得到"添加配置转换"选项,但它不适用于XML文件:

enter image description here

如何转换XML文件?

1 个答案:

答案 0 :(得分:1)

  

不幸的是,它看起来只有* .config文件?!

不正确。您可以转换任何XML文件。例如,在MvcSiteMapProvider中,XML Transformation用于为NuGet构建.nuspec XML文件。

MvcSiteMapProvider.Core.Shared.nuspec

这是一个“基础”文件,后来被转换。

<?xml version="1.0"?>
<!-- MvcSiteMapProvider.MVCx.Core shared Nuget specification file; all packages share this -->
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
    <metadata>
        <id>MvcSiteMapProvider.MVC#mvc_version#.Core</id>
        <title>MvcSiteMapProvider MVC#mvc_version# Core</title>
        <version>$version$</version>
        <authors>Maarten Balliauw, Shad Storhaug</authors>
        <requireLicenseAcceptance>false</requireLicenseAcceptance>
        <summary>MvcSiteMapProvider is a tool that provides flexible menus, breadcrumb trails, and SEO features for the ASP.NET MVC framework, similar to the ASP.NET SiteMapProvider model. The project is hosted on http://github.com/maartenba/MvcSiteMapProvider.</summary>
        <description>This package contains only the core library for MvcSiteMapProvider. Use this package if you need to reference the library from a non-MVC project such as a class library.</description>
        <language>en-US</language>
        <tags>mvc mvc#mvc_version# sitemap mvcsitemap menu breadcrumb navigation mvcsitemapprovider</tags>
        <projectUrl>http://github.com/maartenba/MvcSiteMapProvider</projectUrl>
        <iconUrl>https://raw.githubusercontent.com/maartenba/MvcSiteMapProvider/master/map-icon.gif</iconUrl>
        <dependencies>
            <group targetFramework="net35">
            </group>
            <group targetFramework="net40">
            </group>
            <group targetFramework="net45">
            </group>
        </dependencies>
    </metadata>
</package>

MvcSiteMapProvider.MVC5.Core.nutrans

这是一个转换MVC 5元素的XML转换文件:

<?xml version="1.0"?>
<!-- Nuget specification file for MVC5 -->
<package inherits="MvcSiteMapProvider.Shared.nuspec" xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd" xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <metadata>
    <dependencies>
        <group targetFramework="net45" xdt:Locator="Match(targetFramework)">
            <dependency id="Microsoft.AspNet.Razor" version="[3,4)" xdt:Transform="Insert" />
            <dependency id="Microsoft.AspNet.WebPages" version="[3,4)" xdt:Transform="Insert" />
            <dependency id="Microsoft.AspNet.Mvc" version="[5,6)" xdt:Transform="Insert" />
        </group>
    </dependencies>
  </metadata>
</package>

转化

这是一个用于执行XML转换的MSBuild file named TransformXml.proj

<Project ToolsVersion="4.0" DefaultTargets="Transform" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <UsingTask TaskName="TransformXml"
        AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll"/>

    <PropertyGroup>
        <Source></Source>
        <Transform></Transform>
        <Destination></Destination>
    </PropertyGroup>

    <Target Name="Transform">
        <TransformXml Source="$(Source)"
                  Transform="$(Transform)"
                  Destination="$(Destination)" />
    </Target>
</Project>

有关如何获取Microsoft.Web.Publishing.Tasks.dll的信息,请参阅Which installer installs Microsoft.Web.Publishing.Tasks.dll?

command to execute from PowerShell看起来像是:

function Transform-Nuspec ([string] $source, [string] $transform, [string] $destination) {
    $transform_xml = "$tools_directory\TransformXml.proj"
    Write-Host "Creating nuspec for $destination" -ForegroundColor Green
    Exec { msbuild $transform_xml /p:Source=$source /p:Transform=$transform /p:Destination=$destination /v:minimal /nologo }
    $nuspec
}

MSBuild命令可以从任何类型的可以执行MSBuild.exe的脚本或进程运行。