Home > PowerShell, XML > Powershell functions to get an xml node, and get and set an xml element’s value, even when the element does not already exist

Powershell functions to get an xml node, and get and set an xml element’s value, even when the element does not already exist

I’m new to working with Xml through PowerShell and was so impressed when I discovered how easy it was to read an xml element’s value.  I’m working with reading/writing .nuspec files for working with NuGet.  Here’s a sample xml of a .nuspec xml file:

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
  <metadata>
    <id>MyAppsId</id>
    <version>1.0.1</version>
    <title>MyApp</title>
    <authors>Daniel Schroeder</authors>
    <owners>Daniel Schroeder</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>My App.</description>
    <summary>My App.</summary>
    <tags>Powershell, Application</tags>
  </metadata>
  <files>
    <file src="MyApp.ps1" target="content\MyApp.ps1" />
  </files>
</package>

 

In PowerShell if I want to get the version element’s value, I can just do:

# Read in the file contents and return the version node's value.
[ xml ]$fileContents = Get-Content -Path $NuSpecFilePath
return $fileContents.package.metadata.version

 

Wow, that’s super easy.  And if I want to update that version number, I can just do:

# Read in the file contents, update the version node's value, and save the file.
[ xml ] $fileContents = Get-Content -Path $NuSpecFilePath
$fileContents.package.metadata.version = $NewVersionNumber
$fileContents.Save($NuSpecFilePath)

 

Holy smokes. So simple it blows my mind.  So everything is great, right?  Well, it is until you try and read or write to an element that doesn’t exist.  If the <version> element is not in the xml, when I try and read from it or write to it, I get an error such as “Error: Property ‘version’ cannot be found on this object. Make sure that it exists.”.  You would think that checking if an element exists would be straight-forward and easy right? Well, it almost is.  There’s a SelectSingleNode() function that we can use to look for the element, but what I realized after a couple hours of banging my head on the wall and stumbling across this stack overflow post, is that in order for this function to work properly, you really need to use the overloaded method that also takes an XmlNamespaceManager; otherwise the SelectSingleNode() function always returns null.

So basically you need an extra 2 lines in order to setup an XmlNamespaceManager every time you need to look for a node.  This is a little painful, so instead I created this function that will get you the node if it exists, and return $null if it doesn’t:

function Get-XmlNode([ xml ]$XmlDocument, [string]$NodePath, [string]$NamespaceURI = "", [string]$NodeSeparatorCharacter = '.')
{
	# If a Namespace URI was not given, use the Xml document's default namespace.
	if ([string]::IsNullOrEmpty($NamespaceURI)) { $NamespaceURI = $XmlDocument.DocumentElement.NamespaceURI }	
	
	# In order for SelectSingleNode() to actually work, we need to use the fully qualified node path along with an Xml Namespace Manager, so set them up.
	$xmlNsManager = New-Object System.Xml.XmlNamespaceManager($XmlDocument.NameTable)
	$xmlNsManager.AddNamespace("ns", $NamespaceURI)
	$fullyQualifiedNodePath = "/ns:$($NodePath.Replace($($NodeSeparatorCharacter), '/ns:'))"
	
	# Try and get the node, then return it. Returns $null if the node was not found.
	$node = $XmlDocument.SelectSingleNode($fullyQualifiedNodePath, $xmlNsManager)
	return $node
}

 

And you would call this function like so:

# Read in the file contents and return the version node's value.
[ xml ]$fileContents = Get-Content -Path $NuSpecFilePath
$node = Get-XmlNode -XmlDocument $fileContents -NodePath "package.metadata.version"
if ($node -eq $null) { return $null }
return $fileContents.package.metadata.version

 

So if the node doesn’t exist (i.e. is $null), I return $null instead of trying to access the non-existent element.

So by default this Get-XmlNode function uses the xml’s root namespace, which is what we want 95% of the time.  It also takes a NodeSeparatorCharacter that defaults to a period.  While Googling for answers I saw that many people use the the syntax “$fileContents/package/metadata/version” instead of “$fileContents.package.metadata.version”.  I prefer the dot notation, but for those who like the slash just override the NodeSeparatorCharacter with a slash.

<Update>

Later I found that I also wanted the ability to return back multiple xml nodes; that is, if multiple “version” elements were defined I wanted to get them all, not just the first one.  This is simple; instead of using .SelectSingleNode() we can use .SelectNodes().  In order to avoid duplicating code, I broke the code to get the Xml Namespace Manager and Fully Qualified Node Path out into their own functions.  Here is the rewritten code, with the new Get-XmlNodes function:

function Get-XmlNamespaceManager([ xml ]$XmlDocument, [string]$NamespaceURI = "")
{
    # If a Namespace URI was not given, use the Xml document's default namespace.
	if ([string]::IsNullOrEmpty($NamespaceURI)) { $NamespaceURI = $XmlDocument.DocumentElement.NamespaceURI }	
	
	# In order for SelectSingleNode() to actually work, we need to use the fully qualified node path along with an Xml Namespace Manager, so set them up.
	[System.Xml.XmlNamespaceManager]$xmlNsManager = New-Object System.Xml.XmlNamespaceManager($XmlDocument.NameTable)
	$xmlNsManager.AddNamespace("ns", $NamespaceURI)
    return ,$xmlNsManager		# Need to put the comma before the variable name so that PowerShell doesn't convert it into an Object[].
}

function Get-FullyQualifiedXmlNodePath([string]$NodePath, [string]$NodeSeparatorCharacter = '.')
{
    return "/ns:$($NodePath.Replace($($NodeSeparatorCharacter), '/ns:'))"
}

function Get-XmlNode([ xml ]$XmlDocument, [string]$NodePath, [string]$NamespaceURI = "", [string]$NodeSeparatorCharacter = '.')
{
	$xmlNsManager = Get-XmlNamespaceManager -XmlDocument $XmlDocument -NamespaceURI $NamespaceURI
	[string]$fullyQualifiedNodePath = Get-FullyQualifiedXmlNodePath -NodePath $NodePath -NodeSeparatorCharacter $NodeSeparatorCharacter
	
	# Try and get the node, then return it. Returns $null if the node was not found.
	$node = $XmlDocument.SelectSingleNode($fullyQualifiedNodePath, $xmlNsManager)
	return $node
}

function Get-XmlNodes([ xml ]$XmlDocument, [string]$NodePath, [string]$NamespaceURI = "", [string]$NodeSeparatorCharacter = '.')
{
	$xmlNsManager = Get-XmlNamespaceManager -XmlDocument $XmlDocument -NamespaceURI $NamespaceURI
	[string]$fullyQualifiedNodePath = Get-FullyQualifiedXmlNodePath -NodePath $NodePath -NodeSeparatorCharacter $NodeSeparatorCharacter

	# Try and get the nodes, then return them. Returns $null if no nodes were found.
	$nodes = $XmlDocument.SelectNodes($fullyQualifiedNodePath, $xmlNsManager)
	return $nodes
}

Note the comma in the return statement of the Get-XmlNamespaceManager function.  It took me a while to discover why things broke without it.

</Update>

So once I had this, I decided that I might as well make functions for easily getting and setting the text values of an xml element, which is what is provided here:

function Get-XmlElementsTextValue([ xml ]$XmlDocument, [string]$ElementPath, [string]$NamespaceURI = "", [string]$NodeSeparatorCharacter = '.')
{
	# Try and get the node.	
	$node = Get-XmlNode -XmlDocument $XmlDocument -NodePath $ElementPath -NamespaceURI $NamespaceURI -NodeSeparatorCharacter $NodeSeparatorCharacter
	
	# If the node already exists, return its value, otherwise return null.
	if ($node) { return $node.InnerText } else { return $null }
}

function Set-XmlElementsTextValue([ xml ]$XmlDocument, [string]$ElementPath, [string]$TextValue, [string]$NamespaceURI = "", [string]$NodeSeparatorCharacter = '.')
{
	# Try and get the node.	
	$node = Get-XmlNode -XmlDocument $XmlDocument -NodePath $ElementPath -NamespaceURI $NamespaceURI -NodeSeparatorCharacter $NodeSeparatorCharacter
	
	# If the node already exists, update its value.
	if ($node)
	{ 
		$node.InnerText = $TextValue
	}
	# Else the node doesn't exist yet, so create it with the given value.
	else
	{
		# Create the new element with the given value.
		$elementName = $ElementPath.SubString($ElementPath.LastIndexOf($NodeSeparatorCharacter) + 1)
 		$element = $XmlDocument.CreateElement($elementName, $XmlDocument.DocumentElement.NamespaceURI)		
		$textNode = $XmlDocument.CreateTextNode($TextValue)
		$element.AppendChild($textNode) > $null
		
		# Try and get the parent node.
		$parentNodePath = $ElementPath.SubString(0, $ElementPath.LastIndexOf($NodeSeparatorCharacter))
		$parentNode = Get-XmlNode -XmlDocument $XmlDocument -NodePath $parentNodePath -NamespaceURI $NamespaceURI -NodeSeparatorCharacter $NodeSeparatorCharacter
		
		if ($parentNode)
		{
			$parentNode.AppendChild($element) > $null
		}
		else
		{
			throw "$parentNodePath does not exist in the xml."
		}
	}
}

 

The Get-XmlElementsTextValue function is pretty straight forward; return the value if it exists, otherwise return null.  The Set-XmlElementsTextValue is a little more involved because if the element does not exist already, we need to create the new element and attach it as a child to the parent element.

Here’s an example of calling Get-XmlElementsTextValue:

# Read in the file contents and return the version element's value.
[ xml ]$fileContents = Get-Content -Path $NuSpecFilePath
return Get-XmlElementsTextValue -XmlDocument $fileContents -ElementPath "package.metadata.version"

 

And an example of calling Set-XmlElementsTextValue:

# Read in the file contents, update the version element's value, and save the file.
[ xml ]$fileContents = Get-Content -Path $NuSpecFilePath
Set-XmlElementsTextValue -XmlDocument $fileContents -ElementPath "package.metadata.version" -TextValue $NewVersionNumber
$fileContents.Save($NuSpecFilePath)

 

Note that these 2 functions depend on the Get-XmlNode function provided above.

<Update2 – January 7, 2016>

I have had multiple people ask me for similar functions for getting and setting an element’s Attribute value as well, so here are the corresponding functions for that:

function Get-XmlElementsAttributeValue([ xml ]$XmlDocument, [string]$ElementPath, [string]$AttributeName, [string]$NamespaceURI = "", [string]$NodeSeparatorCharacter = '.')
{
	# Try and get the node. 
	$node = Get-XmlNode -XmlDocument $XmlDocument -NodePath $ElementPath -NamespaceURI $NamespaceURI -NodeSeparatorCharacter $NodeSeparatorCharacter
	
	# If the node and attribute already exist, return the attribute's value, otherwise return null.
	if ($node -and $node.$AttributeName) { return $node.$AttributeName } else { return $null }
}

function Set-XmlElementsAttributeValue([ xml ]$XmlDocument, [string]$ElementPath, [string]$AttributeName, [string]$AttributeValue, [string]$NamespaceURI = "", [string]$NodeSeparatorCharacter = '.')
{
	# Try and get the node. 
	$node = Get-XmlNode -XmlDocument $XmlDocument -NodePath $ElementPath -NamespaceURI $NamespaceURI -NodeSeparatorCharacter $NodeSeparatorCharacter
	
	# If the node already exists, create/update its attribute's value.
	if ($node)
	{ 
		$attribute = $XmlDocument.CreateNode([System.Xml.XmlNodeType]::Attribute, $AttributeName, $NamespaceURI)
		$attribute.Value = $AttributeValue
		$node.Attributes.SetNamedItem($attribute) > $null
	}
	# Else the node doesn't exist yet, so create it with the given attribute value.
	else
	{
		# Create the new element with the given value.
		$elementName = $ElementPath.SubString($ElementPath.LastIndexOf($NodeSeparatorCharacter) + 1)
		$element = $XmlDocument.CreateElement($elementName, $XmlDocument.DocumentElement.NamespaceURI)
		$element.SetAttribute($AttributeName, $NamespaceURI, $AttributeValue) > $null
		
		# Try and get the parent node.
		$parentNodePath = $ElementPath.SubString(0, $ElementPath.LastIndexOf($NodeSeparatorCharacter))
		$parentNode = Get-XmlNode -XmlDocument $XmlDocument -NodePath $parentNodePath -NamespaceURI $NamespaceURI -NodeSeparatorCharacter $NodeSeparatorCharacter
		
		if ($parentNode)
		{
			$parentNode.AppendChild($element) > $null
		}
		else
		{
			throw "$parentNodePath does not exist in the xml."
		}
	}
}

</Update2>

Rather than copy-pasting, you can download all of the functions shown here.

I hope you find this useful and that it saves you some time.  Happy coding!

  1. Carl
    July 22nd, 2013 at 04:52 | #1

    Thanks for the article and code samples. Much appreciated as I was coming across the error described when checking for the existence of an XML element.

    Your code works well and has saved me some time.

    One small point, I think you have some typos in the code block. The parameter type in the function headers appears to have become a number 1″

    e.g.

    function Get-XmlNode(1$XmlDocument, [string]$NodePath, [string]$NamespaceURI = “”, [string]$NodeSeparatorCharacter = ‘.’)
    {…
    }

  2. July 22nd, 2013 at 10:43 | #2

    @Carl
    Thanks Carl, WordPress was just parsing my code improperly and converting it to ‘1’. I’ve updated the code to put spaces around the word xml. All fixed now.

  3. July 29th, 2013 at 09:27 | #3

    Hello,

    Thanks for this post – I’ve happily plundered your code 🙂

    I was wondering if you would be able to add to it and show how to read/write to a SharePoint library of xml files. As far as I know the get-content won’t work. I’ve been trying SPFile.OpenBinary() and OpenBinaryStream() which have read ok, but still no luck saving. My situation is concerns a library of InfoPath form data – stored as xml – which I need to bulk amend.

    Thanks,
    John

  4. July 30th, 2013 at 02:57 | #4

    Hello again,

    I figured out something that works. Not sure if its the best way or not, but it works:

    $stream = $file.OpenBinaryStream()
    $xml = New-Object System.Xml.XmlDocument
    $xml.Load($stream)
    #
    #do stuff to $xml using your functions – thanks
    #
    $xml.Save($stream)
    $outStream = [System.Text.ASCIIEncoding]::ASCII.GetBytes($xml.InnerXml)
    $file.SaveBinary($outStream)

    Thanks again for the post,
    John

  5. January 28th, 2014 at 03:52 | #5

    Thank you very much for this. Saved a lot of frustration and time.

  6. Sambhaji
    July 3rd, 2014 at 23:58 | #6

    Thanks ! It helped me lot!

  7. David
    September 18th, 2014 at 08:20 | #7

    Excellent Blog….this is just the information I needed. I was trying to figure out how to dynamically determine the namespace of an xml file so I could then include it in my xpath for pulling data….couldn’t hard code because I needed to rotate through thousands of xmls with different namespaces. This information was perfect for me and worked. All I really needed to finish my program was this for pulling the default namespace:
    $NamespaceURI = $XmlDocument.DocumentElement.NamespaceURI

    Thanks again!!!

  8. John
    November 19th, 2014 at 16:56 | #8

    How about getting a list of all nodes and all children? I’m the type that would like to see that ‘package’ and/or ‘metadata’ is a node and ‘ID’ is a child of ‘metadata’ but not ‘file’ or ‘package’ and can feed get/set XmlElementsTextValue the right parameters.

  9. Ed
    April 10th, 2015 at 00:06 | #9

    Thanks, really helped on small project and learning more on Posh

  10. Charles
    June 15th, 2015 at 14:34 | #10

    Hi,

    Thanks for the code example – it is helpful. I am struggling to figure out how to Get and Set attributes using this code as the basis. I have been trying to adapt the Get-XmlElementsTextValue and the Set-XmlElementsTextValue functions to make new functions Get-XmlElementAttributesValue and Set-XmlElementAttributesValue with no success. I am thinking that I should be able to use the $node to select the attribute. That has not been successful.

    How can I get to the password attribute of a xml path of configuration.servers.server? In the example below I want to change the password attribute value.

    <server name="servername" id="0" username="TheUserName" password="ThePassword"

  11. January 7th, 2016 at 15:06 | #11

    @Charles
    Hi Charles, I’ve just updated this post to include functions for getting and setting an attribute’s value as well. Sorry it took so long 😛 Enjoy!

  12. Charles
    January 7th, 2016 at 17:53 | #12

    @deadlydog – Thanks and wonderful surprise in my inbox today!! I will circle back to what I did on that project and see how I can improve it with this code. Thank You!

  13. dragon788
    November 11th, 2016 at 13:19 | #13

    This is pretty awesome. I was just trying to figure out how to query the version from a nuspec file to append a build number in my AppVeyor config for a Chocolatey package and after some failures with Select-Xml I ran across your awesome post.

  14. Renzo
    January 12th, 2017 at 08:27 | #14

    Hello,
    I have an XML file that includes comments (much more readable). However this is causing havoc with my processing logic.
    Part of the logic uses $SequenceCount = ($CFG.APP.INSTALL.ChildNodes).Count.
    I either need to DELETE comments from the XML object or, alternatively, I need the count to EXCLUDE comments. Can you help??

  15. Renzo
    January 12th, 2017 at 09:21 | #15

    Found the answer!
    ($Test.App.ChildNodes |Where-Object { ‘#comment’ -contains $_.Name }) | ForEach-Object {
    # Remove each node from its parent
    [void]$_.ParentNode.RemoveChild($_)

  16. R
    February 25th, 2017 at 14:17 | #16

    Totally copying (and giving credit to) your script! Just saved me reinventing the wheel, appreciated! Great work and thanks for sharing

  17. Gary
    March 7th, 2017 at 14:13 | #17

    I use your XML functions all of the time and they are great, but I hit a wall. Any idea how to parse an Excel XML file?

    [xml]$xmlInvoice = Get-Content $x.Fullname
    $dataNodes = Get-XmlNodes -XmlDocument $xmlInvoice -NodePath “ss:Workbook.ss:Worksheet.ss:Table.ss:Row.ss:Cell.ss:Data”
    ForEach ( $node in $dataNodes )
    {

    }

    Exception calling “SelectNodes” with “2” argument(s): “‘/ns:ss:Workbook/ns:ss:Worksheet/ns:ss:Table/ns:ss:Row/ns:ss:Cell/ns:ss:Data’
    has an invalid token.”
    At H:\My Documents\Quality Assurance\STPr In Progress\Foodservice Pro Points\Invoice Data Processing\External-Excel-Processing.ps1:33
    char:2
    + $nodes = $XmlDocument.SelectNodes($fullyQualifiedNodePath, $xmlNsManager)
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

  18. October 13th, 2017 at 11:32 | #18

    Thank you. Excellent blog and XML function library. I’ll be using it today!

  19. Me
    January 11th, 2018 at 03:12 | #19

    Thanks a ton!

  1. No trackbacks yet.