Create New XML Item Programmatically

by Mar 30, 2016

In a previous tip we explained how you can clone an existing XML item to add a new item. This works only when there are already some items in the XML file that you can clone. To add new items from scratch, you can also programmatically add items.

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 how you can programmatically add an entry for "NewServer":

# 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)

$machines = Select-Xml -Xml $xml -XPath '//Machines'
$newItem = $xml.CreateElement('Machine')

$info = $xml.CreateElement('IP')
$info.InnerText = '10.10.10.10'
$null = $newItem.AppendChild($info)

$info = $xml.CreateElement('Name')
$info.InnerText = 'NewServer'
$null = $newItem.AppendChild($info)

$info = $xml.CreateElement('Datum')
$info.InnerText = Get-Date -format 'yyyy-MM-dd'
$null = $newItem.AppendChild($info)

$null = $machines.Node.AppendChild($newItem)

$xml.Save($Path)

notepad $Path

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!