title: Remove XML node if date condition met
body: <p>I have an input XML file and I want to delete the node <code>&lt;EndDate&gt;</code> under <code>&lt;AddressRow&gt;</code> if the EndDate is before 01-01-1995.</p>
  
  <p>This is a sample XML file which meets the criteria.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;OrganisationUnits&gt;
      &lt;OrganisationUnitsRow num="1"&gt;
          &lt;OrganisationId&gt;TEST1&lt;/OrganisationId&gt;
          &lt;OrganisationName&gt;TEST PROVIDER&lt;/OrganisationName&gt;
          &lt;Addresses&gt;
            &lt;AddressesRow num="1"&gt;
              &lt;AddressId&gt;G72261&lt;/AddressId&gt;
              &lt;MainAddress&gt;N&lt;/MainAddress&gt;
              &lt;NonStandardAddress&gt;
                &lt;LocationId&gt;P1031587&lt;/LocationId&gt;
                &lt;StreetNumber&gt;20&lt;/StreetNumber&gt;
                &lt;Street&gt;UNION ROAD&lt;/Street&gt;
                &lt;Town&gt;SOLIHULL&lt;/Town&gt;
                &lt;PostCode&gt;B90 3DQ&lt;/PostCode&gt;
              &lt;/NonStandardAddress&gt;
              &lt;StartDate&gt;1970-12-10&lt;/StartDate&gt;
              &lt;EndDate&gt;1994-12-11&lt;/EndDate&gt;
              &lt;AddressType/&gt;
            &lt;/AddressesRow&gt;
          &lt;/Addresses&gt;
          &lt;AcPayRef2&gt;294288&lt;/AcPayRef2&gt;
          &lt;Obsolete&gt;N&lt;/Obsolete&gt;
      &lt;/OrganisationUnitsRow&gt;
  &lt;/OrganisationUnits&gt;
  </code></pre>
  
  <p>If figured out how to read in the file and delete the node ok which works fine.</p>
  
  <pre><code>    #Loop through the xml file and look for the node &lt;EndDate&gt; that exists under CMCOrganisationUnits.CMCOrganisationUnitsRow.Addresses.CMCAddressesRow
  foreach ($CMCAddressesRow in $xml.CMCOrganisationUnits.CMCOrganisationUnitsRow.Addresses.CMCAddressesRow) 
      {
      #set a variable for the the node that we want to remove
      $n = $CMCAddressesRow.Item('EndDate')
  
      #if the node (EndDate) does exist 
      if ($n) {
  
          #it exists then delete the parent node also
          #$n.ParentNode.ParentNode.RemoveChild($n.ParentNode)
          }
      }
      #save the changes to the file
      $xml.Save("$path\$xml_out")
  </code></pre>
  
  <p>So this would delete it whether there was a date there or not. What I cant figure out is how to test the date value. I did try to use a callback from the main loop like this without success.</p>
  
  <pre><code>#Check the End date minimum value here
  $adr_end_callback = {
    param($match)
    $current = [DateTime]$match.Groups[1].Value
    $minimum = [DateTime]'1995-01-01'
  
        if ($minimum -gt $current)
        {
          '&lt;EndDate&gt;1995-01-01&lt;/EndDate&gt;'
        }
        else {
      '&lt;EndDate&gt;' + $match.Groups[1].Value + '&lt;/EndDate&gt;'
        }
      }
  </code></pre>
  
  <p>Maybe I'm overthinking it and there is a simple answer to it.</p>