Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web.Config transforms outside of Microsoft MSBuild?

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)\MSBuild\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll which requires a build engine. Thanks

like image 397
What Would Be Cool Avatar asked Jan 24 '12 15:01

What Would Be Cool


People also ask

How does web config transform work?

A Web. config transformation file contains XML markup that specifies how to change the Web. config file when it is deployed. You can specify different changes for specific build configurations and for specific publish profiles.

How do you create a web config for different environments?

In order to create the web. config transformations, locate the web. config file in the ASP.NET project, and right-click the item in the solution explorer. Notice the menu item “Add Config Transforms”.

What is Xdt transformation?

XDT is a simple and straight forward method of transforming the web. config during publishing/packaging. Transformation actions are specified using XML attributes defined in the XML-Document-Transform namespace, that is mapped to the xdt prefix.

Where is web config file in Visual Studio 2022?

config file is located in the %SystemRoot%\Microsoft.NET\Framework\%VersionNumber%\CONFIG\ folder. The default settings that are contained in the Machine.


1 Answers

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

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 "$scriptPath\Microsoft.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); } 

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

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

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

like image 155
Mike DaCosta Avatar answered Oct 19 '22 02:10

Mike DaCosta