Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a child element for XML in PowerShell

Tags:

powershell

xml

I'm trying to create a child XML element for this xml:

<?xml version="1.0" encoding="utf-8"?> <configuration> </configuration> 

I use this PowerShell script:

[xml] $doc = Get-Content($filePath) $child = $doc.CreateElement("newElement") $doc.configuration.AppendChild($child) 

I have an error: Method invocation failed because [System.String] doesn't contain a method named 'AppendChild'.

like image 957
Warlock Avatar asked Oct 08 '13 10:10

Warlock


People also ask

How do I add an XML tag in PowerShell?

So we will first load the XML file and then operate on it as shown below. The below command will save the XML file to the variable. Once the element is created we need to append it to the specific node. Here we need to append a new element underneath the Catalog.

Which function is used to add child element to the XML node?

The appendChild() method adds a child node to an existing node. The new node is added (appended) after any existing child nodes.

What is an XML child element?

An XML tree starts at a root element and branches from the root to child elements. All elements can have sub elements (child elements): <root> <child>

How do I get the contents of a XML file in PowerShell?

One way to read an XML document in PowerShell is to typecast a variable to the type [xml]. To create this variable, we can use the Get-Content cmdlet to read all of the text in an XML document. To typecast the output of Get-Content we can simply prepend the text [xml] before the variable.


1 Answers

If you use dot notation to navigate an XML file (e.g. $doc.configuration), Powershell tries to be clever about what it returns.

  • If the target element is empty or only contains a single text node, PS will return a String.
  • If the target element contains child nodes other than text nodes, it will return an XmlElement.
  • If multiple target elements exist, it will return an Object[], where each individual array element is again subject to these rules, e.g. it will either be a String or an XmlElement depending on its contents.
  • If the target element does not exist, PS returns $null.

In your case it's easy since you want to append nodes to the document element:

$doc = New-Object System.Xml.XmlDocument $doc.Load($filePath) $child = $doc.CreateElement("newElement") $doc.DocumentElement.AppendChild($child) 

but you could use $doc.SelectNodes() or $doc.SelectSingleNode() to navigate around the XML document and always have a node/node list returned.


One could argue about the sensibility of this behavior, but as a matter of fact it makes consuming (sanely structured) XML quite straight-forward - for example tasks such as reading values from a config file, or from an API response. That's the purpose of this simple syntax.

It's not a good tool for creating XML, which is a more complex task. Using DOM API methods from the start is the better approach here.

like image 194
Tomalak Avatar answered Oct 04 '22 20:10

Tomalak