Web.Config 在 Microsoft MSBuild 之外进行转换? [英] Web.Config transforms outside of Microsoft MSBuild?

查看:33
本文介绍了Web.Config 在 Microsoft MSBuild 之外进行转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 MSBuild 之外使用 Microsoft 的 XML 文档转换来准备 web.configs?我想使用 PowerShell 进行这些转换,而不必通过 MSBuild 引擎运行它.如果 Microsoft 使用标准 XSLT,那么在 PowerShell 中就很容易做到.据我所知,我必须使用他们的 C:Program Files (x86)MSBuildMicrosoftVisualStudiov10.0WebMicrosoft.Web.Publishing.Tasks.dll,它需要一个构建引擎.谢谢

Is it possible to use Microsoft's XML document transform, for preparing web.configs, outside of MSBuild? I would like to use PowerShell to do these transform without having to run this through the MSBuild engine. If Microsoft had used standard XSLT it would be easy to do in PowerShell. From what I can tell I have to use their C:Program Files (x86)MSBuildMicrosoftVisualStudiov10.0WebMicrosoft.Web.Publishing.Tasks.dll which requires a build engine. Thanks

推荐答案

我创建了一个小函数来在 PowerShell 中处理 Microsoft 的 XML 文档转换.

I created a small function to handle Microsoft's XML Document Transform in PowerShell.

我将 Microsoft.Web.XmlTransform.dll 文件从 Visual Studio 构建文件夹复制到我的脚本路径,但如果您愿意,可以从源文件夹中引用它.

I copied the Microsoft.Web.XmlTransform.dll file from Visual Studio build folder to my script's path, but you can reference it from the source folder if you'd like.

function XmlDocTransform($xml, $xdt)
{
    if (!$xml -or !(Test-Path -path $xml -PathType Leaf)) {
        throw "File not found. $xml";
    }
    if (!$xdt -or !(Test-Path -path $xdt -PathType Leaf)) {
        throw "File not found. $xdt";
    }

    $scriptPath = (Get-Variable MyInvocation -Scope 1).Value.InvocationName | split-path -parent
    Add-Type -LiteralPath "$scriptPathMicrosoft.Web.XmlTransform.dll"

    $xmldoc = New-Object Microsoft.Web.XmlTransform.XmlTransformableDocument;
    $xmldoc.PreserveWhitespace = $true
    $xmldoc.Load($xml);

    $transf = New-Object Microsoft.Web.XmlTransform.XmlTransformation($xdt);
    if ($transf.Apply($xmldoc) -eq $false)
    {
        throw "Transformation failed."
    }
    $xmldoc.Save($xml);
}

使用 web.release.config 转换 web.config:

To transform web.config using web.release.config:

XmlDocTransform -xml "Web.config" -xdt "Web.Release.config"

或者,您可以使用 Sayed 的自引导 Xml 转换脚本,该脚本将负责为您获取 Microsoft.Xml.Xdt.dll:

Alternatively, you can use Sayed's self-bootstraping Xml Transform script, which will take care of getting the Microsoft.Xml.Xdt.dll for you:

https://gist.github.com/sayedihashimi/f1fdc4bfba74d398ec5b

这篇关于Web.Config 在 Microsoft MSBuild 之外进行转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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