Adding New Nodes to an XML Document

by Mar 28, 2016

If you need to add new items to an XML document that already contains such items, the easiest way is to search for an existing item, then clone it. You can then update the information in the cloned item and insert it into the XML document.

Before you play: In a previous tip we showed how to create a new XML file from scratch. This XML document is used in the next example. If you missed the tip, you find the content of the XML file below. Make sure you save it into $env:temp\inventory.xml.

Let's see what it takes to add a new machine to the inventory data and save the updated XML to a new file:

# this is where the XML sample file was saved
$Path = "$env:temp\inventory.xml"
 
# load it into an XML object
$xml = New-Object -TypeName XML
$xml.Load($Path)

# clone an existing node structure
$item = Select-Xml -Xml $xml -XPath '//Machine[1]'
$newnode = $item.Node.CloneNode($true)
 
# update the information as needed
# all other information is defaulted to the values from the original node
$newnode.Name = 'NewServer'
$newnode.IP = '1.2.3.4'
 
# get the node you want the new node to be appended to
$machines = Select-Xml -Xml $xml -XPath '//Machines'
$machines.Node.AppendChild($newnode)
 
$NewPath = "$env:temp\inventory2.xml"

$xml.Save($NewPath)

notepad $NewPath

Here is the sample XML document:

<?xml version="1.0"?>
<?xml-stylesheet type='text/xsl' href='style.xsl'?>
<!–List of machines–>
<Machines current="True" manager="Tobias">
          <!–1. machine details–>
          <Machine test="940854388">
                   <Name>Server0001</Name>
                   <IP>40.101.105.27</IP>
                   <GUID>8c49e799-b661-4076-b325-f030d25b299e</GUID>
                   <Information info1="some info" info2="more info">RawContent</Information>
                   <CodeSegment info3="another attribute"><![CDATA[this is untouched code and can contain special characters /\@<>]]></CodeSegment>
          </Machine>
          <!–2. machine details–>
          <Machine test="1567577618">
                   <Name>Server0002</Name>
                   <IP>161.29.237.75</IP>
                   <GUID>eca864a0-28a6-4e60-9438-c58b3f4c5b91</GUID>
                   <Information info1="some info" info2="more info">RawContent</Information>
                   <CodeSegment info3="another attribute"><![CDATA[this is untouched code and can contain special characters /\@<>]]></CodeSegment>
          </Machine>
          <!–3. machine details–>
          <Machine test="1385488736">
                   <Name>Server0003</Name>
                   <IP>202.222.193.169</IP>
                   <GUID>487b0b85-d03b-4fe1-bec1-895bb4c6f43d</GUID>
                   <Information info1="some info" info2="more info">RawContent</Information>
                   <CodeSegment info3="another attribute"><![CDATA[this is untouched code and can contain special characters /\@<>]]></CodeSegment>
          </Machine>
          <!–4. machine details–>
          <Machine test="1141441915">
                   <Name>Server0004</Name>
                   <IP>115.249.246.167</IP>
                   <GUID>917a1243-c354-41d8-aa8e-5b9bf0334a80</GUID>
                   <Information info1="some info" info2="more info">RawContent</Information>
                   <CodeSegment info3="another attribute"><![CDATA[this is untouched code and can contain special characters /\@<>]]></CodeSegment>
          </Machine>
          <!–5. machine details–>
          <Machine test="2134573729">
                   <Name>Server0005</Name>
                   <IP>239.97.156.231</IP>
                   <GUID>f961916d-ccfb-4395-8e61-59299ef11926</GUID>
                   <Information info1="some info" info2="more info">RawContent</Information>
                   <CodeSegment info3="another attribute"><![CDATA[this is untouched code and can contain special characters /\@<>]]></CodeSegment>
          </Machine>
          <!–6. machine details–>
          <Machine test="1569812933">
                   <Name>Server0006</Name>
                   <IP>66.5.84.251</IP>
                   <GUID>154e5e92-f063-4571-8987-2d3e76b34331</GUID>
                   <Information info1="some info" info2="more info">RawContent</Information>
                   <CodeSegment info3="another attribute"><![CDATA[this is untouched code and can contain special characters /\@<>]]></CodeSegment>
          </Machine>
          <!–7. machine details–>
          <Machine test="426025640">
                   <Name>Server0007</Name>
                   <IP>99.140.184.148</IP>
                   <GUID>0568c635-8bd9-4834-8084-28e1fd78d512</GUID>
                   <Information info1="some info" info2="more info">RawContent</Information>
                   <CodeSegment info3="another attribute"><![CDATA[this is untouched code and can contain special characters /\@<>]]></CodeSegment>
          </Machine>
          <!–8. machine details–>
          <Machine test="1706184427">
                   <Name>Server0008</Name>
                   <IP>129.242.172.7</IP>
                   <GUID>b106eff3-5642-4857-a12d-53d6f552bf07</GUID>
                   <Information info1="some info" info2="more info">RawContent</Information>
                   <CodeSegment info3="another attribute"><![CDATA[this is untouched code and can contain special characters /\@<>]]></CodeSegment>
          </Machine>
          <!–9. machine details–>
          <Machine test="1724935941">
                   <Name>Server0009</Name>
                   <IP>165.234.115.96</IP>
                   <GUID>e72b07ae-e5fa-49a9-a663-f17b4894c50d</GUID>
                   <Information info1="some info" info2="more info">RawContent</Information>
                   <CodeSegment info3="another attribute"><![CDATA[this is untouched code and can contain special characters /\@<>]]></CodeSegment>
          </Machine>
          <!–10. machine details–>
          <Machine test="154244134">
                   <Name>Server0010</Name>
                   <IP>191.162.109.0</IP>
                   <GUID>f4568d8d-2302-404c-8c0e-1f7047cf8a1a</GUID>
                   <Information info1="some info" info2="more info">RawContent</Information>
                   <CodeSegment info3="another attribute"><![CDATA[this is untouched code and can contain special characters /\@<>]]></CodeSegment>
          </Machine>
</Machines>

Twitter This Tip! ReTweet this Tip!