title: Powershell - Add New Data to XML
body: <p>I have a part of the script that calls an API for the attributes of the host. If API fails, it checks if there is text file containing the attributes (from a previous run). If the file does not exist, I need to add UNKNOWN to the attributes node. See the expected out come in the XML below. The XML will start of with no attributes as the script is meant to populate this info.  </p>
  
  <pre><code>  &lt;?xml version="1.0" encoding="ISO-8859-1" ?&gt; 
  - &lt;netprobe compatibility="1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://schema.itrsgroup.com/GA2011.2-110303/netprobe.xsd"&gt;
  - &lt;selfAnnounce&gt;
    &lt;enabled&gt;true&lt;/enabled&gt; 
    &lt;retryInterval&gt;60&lt;/retryInterval&gt; 
    &lt;requireReverseConnection&gt;false&lt;/requireReverseConnection&gt; 
  - &lt;probeName&gt;
    &lt;hostname /&gt; 
    &lt;data&gt;_&lt;/data&gt; 
    &lt;port /&gt; 
    &lt;data&gt;-SA&lt;/data&gt; 
    &lt;/probeName&gt;
  - &lt;managedEntity&gt;
    &lt;name&gt;TestBox&lt;/name&gt; 
  - &lt;attributes&gt;
    &lt;attribute name="Business Unit"&gt;UNKNOWN&lt;/attribute&gt; 
    &lt;attribute name="Department"&gt;UNKNOWN&lt;/attribute&gt; 
    &lt;attribute name="Team"&gt;UNKNOWN&lt;/attribute&gt; 
    &lt;attribute name="Environment"&gt;UNKNOWN&lt;/attribute&gt; 
    &lt;attribute name="ServerModel"&gt;UNKNOWN&lt;/attribute&gt; 
    &lt;attribute name="datacentre"&gt;UNKNOWN&lt;/attribute&gt; 
    &lt;attribute name="Application"&gt;UNKNOWN&lt;/attribute&gt; 
    &lt;attribute name="Description"UNKNOWN&lt;/attribute&gt; 
    &lt;/attributes&gt;
    &lt;types&gt;
    &lt;/types&gt;
      &lt;/managedEntity&gt;
      &lt;gateways&gt;
      &lt;/gateway&gt;
      &lt;/gateways&gt;
    &lt;/selfAnnounce&gt;
  &lt;/netprobe&gt;
  </code></pre>
  
  <p>How do I add the data? I thought it would be something like this which I would run for each attribute name</p>
  
  <pre><code>[xml]$XML = Get-Content C:\selfannounce.xml
  $tempchild = $xml.CreateElement("attribute")
  $tempchild.InnerText = "Unknown"
  $a = $XML.SelectSingleNode("//attributes")
  $a.AppendChild($tempchild)
  
  [xml]$XML.Save("C:\selfannouncetest.xml")
  </code></pre>
  
  <p>But this gives me the below.</p>
  
  <pre><code>  &lt;attributes&gt;
      &lt;attribute&gt;Unknown&lt;/attribute&gt;
    &lt;/attributes&gt;
  </code></pre>
  
  <p>I dont know how to get the "attribute name=" part.</p>
  
  <pre><code>&lt;attribute name="Business Unit"&gt;UNKNOWN&lt;/attribute&gt; 
  </code></pre>
  
  <p>Anyone know the best way to achieve this? It will be added to the following code </p>
  
  <pre><code>Try {
  $API = New-Object System.Net.WebClient
  $APIData = $API.DownloadString($FullURL)
  Set-Content -Value $APIdata -Path $APIDataFile -Force
  #$Newdata = Get-Content C:\API.txt
  }
  catch [Net.WebException] {
  $CheckAPIFile = Test-Path $APIDataFile 
  If ($CheckAPiFile -eq $true){
  $APIdata = Get-Content $APIDataFile
  }
  Else {
  
  #Create attributes and add UNKNOWN
  
  } 
  }
  </code></pre>
  
  <p>And also, how can I add the below to the logic if the API call is successful or text file exists? Not sure where to insert it in the above script</p>
  
  <pre><code>[xml]$XML = Get-Content $SelfannounceXMLEdit
  ($APIData -split "`n") | Where-Object { $_.Trim() } | ForEach-Object {
      $tempchild = [xml]$_.Trim()
      $attribute = $xml.ImportNode($tempchild.attribute, $true)
      $newType = $XML.netprobe.selfAnnounce.managedEntity.selectsinglenode("attributes").AppendChild($attribute)
  }
  $XML.Save($SelfannounceXMLEdit)
  </code></pre>
  
  <p>Please note, must work on PowerShell 2.0.</p>