如何强制对xml文件进行配置转换? [英] how to force config transform on an xml file?

查看:67
本文介绍了如何强制对xml文件进行配置转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚为VS2017安装了

如何转换XML文件?

解决方案

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

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

 <?xml version ="1.0"?><!-MvcSiteMapProvider.MVCx.Core共享Nuget规范文件;所有软件包都共享此->< package xmlns ="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd"><元数据>< id> MvcSiteMapProvider.MVC#mvc_version#.Core</id>< title> MvcSiteMapProvider MVC#mvc_version#Core</title>< version> $ version $</version>< authors> Shad Storhaug的Maarten Balliauw</authors>< requireLicenseAcceptance> false</requireLicenseAcceptance>< summary> MvcSiteMapProvider是一种工具,可为ASP.NET MVC框架提供灵活的菜单,痕迹和SEO功能,类似于ASP.NET SiteMapProvider模型.该项目位于http://github.com/maartenba/MvcSiteMapProvider.<说明>该软件包仅包含MvcSiteMapProvider的核心库.如果您需要引用非MVC项目中的库(例如类库),请使用此包.< language> zh-CN</language><标签> mvc mvc#mvc_version#站点地图mvcsitemap菜单面包屑导航mvcsitemapprovider</tags>< projectUrl> http://github.com/maartenba/MvcSiteMapProvider</projectUrl>< iconUrl> https://raw.githubusercontent.com/maartenba/MvcSiteMapProvider/master/map-icon.gif</iconUrl><依赖关系>< group targetFramework ="net35"></group>< group targetFramework ="net40"></group>< group targetFramework ="net45"></group></dependencies></元数据></package> 

..nu​​trans

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

 <?xml version ="1.0"?><!-MVC5的Nuget规范文件->< packageInherits ="MvcSiteMapProvider.Shared.nuspec" xmlns ="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd" xmlns:xdt ="http://schemas.microsoft.com/XML文档转换"<元数据><依赖关系>< 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></元数据></package> 

转化

这是一个名为 TransformXml.MSBuild的MSBuild文件./code> 用于执行XML转换:

 < 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><目的地>/目的地</PropertyGroup><目标名称=转换">< TransformXml Source ="$(Source)"Transform ="$(Transform)"Destination ="$(目的地)"/></Target></Project> 

请参阅哪个安装程序将安装Microsoft.Web.Publishing.Tasks.dll?有关如何获取 Microsoft.Web.Publishing.Tasks.dll 的信息.

可从PowerShell执行的命令看起来像:

  function Transform-Nuspec([string] $ source,[string] $ transform,[string] $ destination){$ transform_xml ="$ tools_directory \ TransformXml.proj"写主机为$ destination创建nuspec" -ForegroundColor绿色执行{msbuild $ transform_xml/p:Source = $ source/p:Transform = $ transform/p:Destination = $ destination/v:minimum/nologo}$ nuspec} 

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

I've just installed Configuration Transform for VS2017, hoping to transform some XML files.

Unfortunately, it looks like it only does *.config files?!

For example, when I write click on a *.config file, I get the "add config transform" option, but it's not available for XML files:

How do I transform an XML file?

解决方案

Unfortunately, it looks like it only does *.config files?!

Not true. You can transform any XML file. For example, in MvcSiteMapProvider, XML Transformation is used to build the .nuspec XML files for NuGet.

MvcSiteMapProvider.Core.Shared.nuspec

This is a "base" file that is later transformed.

<?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

Here is an XML transformation file that converts elements for MVC 5:

<?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>

Transformation

Here is an MSBuild file named TransformXml.proj that is used to execute the XML transform:

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

See Which installer installs Microsoft.Web.Publishing.Tasks.dll? for information about how to obtain Microsoft.Web.Publishing.Tasks.dll.

And the command to execute from PowerShell looks like:

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
}

The MSBuild command could be run from any type of script or process that can execute MSBuild.exe.

这篇关于如何强制对xml文件进行配置转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆