Skip to content

Instantly share code, notes, and snippets.

@thoemmi
Created June 15, 2012 15:50
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thoemmi/2937183 to your computer and use it in GitHub Desktop.
Save thoemmi/2937183 to your computer and use it in GitHub Desktop.
PowerShell function to add an XML fragment to an XmlNode
<#
.SYNOPSIS
Adds an XML fragment to an XmlNode
.DESCRIPTION
Adds an XML fragment to an XmlNode
.NOTES
Author : Thomas Freudenberg - info@thomasfreudenberg.com
.EXAMPLE
Add-XmlFragment $xml.configuration.connectionStrings "<add name='MyConString' connectionString='...' providerName='...'>"
Adds the connection string "MyConString" to the web.config file.
#>
function Add-XmlFragment {
Param(
[Parameter(Mandatory=$true)][System.Xml.XmlNode] $xmlElement,
[Parameter(Mandatory=$true)][string] $text)
$xml = $xmlElement.OwnerDocument.ImportNode(([xml]$text).DocumentElement, $true)
[void]$xmlElement.AppendChild($xml)
}
@arviman
Copy link

arviman commented May 30, 2016

Excellent. This works really well!

@tioxod
Copy link

tioxod commented May 1, 2017

This realization will not work in case of empty node $xml.configuration.connectionStrings

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment