title: Adding and Removing Xmlnode using Powershell
body: <p>I am trying to add and remove elements from multiple xml files. i ran into two issue. it removed empty elements automatically and second it did not remove what i wanted to</p>
  
  <p>I have my xml like this</p>
  
  <pre><code>&lt;Entity&gt;
     &lt;App&gt;
        &lt;item1&gt;1&lt;/item1&gt;
        &lt;emptyItem/&gt;
        &lt;person&gt;
           &lt;itemToRemove&gt;true&lt;/itemToRemove&gt;
           &lt;emptyItem/&gt;
           &lt;otheritem&gt;1&lt;/otheritem&gt;
        &lt;/person&gt;
        &lt;person&gt;
           &lt;itemToRemove&gt;false&lt;/itemToRemove&gt;
           &lt;emptyItem/&gt;
           &lt;otheritem&gt;3&lt;/otheritem&gt;
        &lt;/person&gt;
        &lt;person&gt;
           &lt;itemToRemove&gt;false&lt;/itemToRemove&gt;
           &lt;emptyItem/&gt;
           &lt;otheritem&gt;3&lt;/otheritem&gt;
        &lt;/person&gt;
     &lt;/App&gt;
  &lt;/Entity&gt;
  </code></pre>
  
  <p>what i want is</p>
  
  <pre><code>&lt;Entity&gt;
     &lt;App&gt;
        &lt;item1&gt;1&lt;/item1&gt;
        &lt;emptyItem/&gt;
        &lt;person&gt;
  
           &lt;emptyItem/&gt;
           &lt;otheritem&gt;1&lt;/otheritem&gt;
        &lt;/person&gt;
        &lt;person&gt;
  
           &lt;emptyItem/&gt;
           &lt;otheritem&gt;3&lt;/otheritem&gt;
        &lt;/person&gt;
        &lt;person&gt;
  
           &lt;emptyItem/&gt;
           &lt;otheritem&gt;3&lt;/otheritem&gt;
        &lt;/person&gt;
        &lt;newItemtoAdd&gt;2001-01-01&lt;/newItemtoAdd&gt;
     &lt;/App&gt;
  &lt;/Entity&gt;
  </code></pre>
  
  <p><strong>i added newItemtoAdd element and removed itemToRemove on output xml above.</strong></p>
  
  <pre><code>with the current script i have used what i get is this
  
  &lt;Entity&gt;
     &lt;App&gt;
        &lt;item1&gt;1&lt;/item1&gt;
        &lt;emptyItem/&gt;
        &lt;person&gt;
           &lt;itemToRemove&gt;true&lt;/itemToRemove&gt;
           &lt;otheritem&gt;1&lt;/otheritem&gt;
        &lt;/person&gt;
        &lt;person&gt;
           &lt;itemToRemove&gt;false&lt;/itemToRemove&gt;
           &lt;otheritem&gt;3&lt;/otheritem&gt;
        &lt;/person&gt;
        &lt;person&gt;
           &lt;itemToRemove&gt;false&lt;/itemToRemove&gt;
           &lt;otheritem&gt;3&lt;/otheritem&gt;
        &lt;/person&gt;
        &lt;newItemtoAdd&gt;2001-01-01&lt;/newItemtoAdd&gt;
     &lt;/App&gt;
  &lt;/Entity&gt;
  </code></pre>
  
  <p><strong>emptyItem node is removed which i do not want to happen, newItemtoAdd is added which is good, and itemToRemove node is not removed which is not what i want</strong></p>
  
  <p>here is my script</p>
  
  <pre><code>Get-ChildItem D:\Projects\*.xml | 
      % { 
          [xml]$xml      = [xml](Get-Content $_.fullname)
          $newItemtoAdd = $xml.CreateElement('newItemtoAdd')
          $newItemtoAdd.PsBase.InnerText = '1900-01-01'
          $null     = $xml.Entity.App.AppendChild($newItemtoAdd)
  
      $xml.SelectNodes("//Entity/App/person") | ? {
      $_.name -eq "itemToRemove"   } | % {$_.ParentNode.RemoveChildNode($_) }
         $xml.Save($_.FullName)
      }
  </code></pre>