Avi Shilon
Avi Shilon

Reputation: 293

Remove specific xml elements using PowerShell

I have the following xml:

<NS1:PriceMaintenanceRequest xmlns:NS1="http://retalix.com/R10/services" MajorVersion="2" MinorVersion="0" FixVersion="0">
  <NS1:Header>
    <NS1:MessageId>1234</NS1:MessageId>
    <NS1:Bulk UnitOfWork="false"/>
  </NS1:Header>
  <NS1:ProductPrice BusinessUnitId="9200" Action="AddOrUpdate">
    <NS1:ProductId>2840000133</NS1:ProductId>
    <NS1:Price Action="AddUpdate" Sequence="1234">
      <NS1:UnitOfMeasureCode>EA</NS1:UnitOfMeasureCode>
      <NS1:EffectiveDateTimestamp>2012-09-20T00:00:00</NS1:EffectiveDateTimestamp>
      <NS1:EndDateTimestamp>2056-12-12T23:59:00</NS1:EndDateTimestamp>
      <NS1:CatalogPrice Currency="USD">4.2900</NS1:CatalogPrice>
      <NS1:BatchID>491364c5-73f5-45a4-8355-79cc0a720ea0</NS1:BatchID>
      <NS1:ChangeStatus>NA</NS1:ChangeStatus>
    </NS1:Price>
  </NS1:ProductPrice>
  <NS1:ProductPrice BusinessUnitId="90" Action="AddOrUpdate">
    <NS1:ProductId>2840000133</NS1:ProductId>
    <NS1:Price Action="AddUpdate" Sequence="1234">
      <NS1:UnitOfMeasureCode>EA</NS1:UnitOfMeasureCode>
      <NS1:EffectiveDateTimestamp>2012-09-20T00:00:00</NS1:EffectiveDateTimestamp>
      <NS1:EndDateTimestamp>2056-12-12T23:59:00</NS1:EndDateTimestamp>
      <NS1:CatalogPrice Currency="USD">4.2900</NS1:CatalogPrice>
      <NS1:BatchID>491364c5-73f5-45a4-8355-79cc0a720ea0</NS1:BatchID>
      <NS1:ChangeStatus>NA</NS1:ChangeStatus>
    </NS1:Price>
  </NS1:ProductPrice>
  <NS1:ProductPrice BusinessUnitId="90" Action="AddOrUpdate">
    <NS1:ProductId>2840000133</NS1:ProductId>
    <NS1:Price Action="AddUpdate" Sequence="1234">
      <NS1:UnitOfMeasureCode>EA</NS1:UnitOfMeasureCode>
      <NS1:EffectiveDateTimestamp>2012-09-20T00:00:00</NS1:EffectiveDateTimestamp>
      <NS1:EndDateTimestamp>2056-12-12T23:59:00</NS1:EndDateTimestamp>
      <NS1:CatalogPrice Currency="USD">4.2900</NS1:CatalogPrice>
      <NS1:BatchID>491364c5-73f5-45a4-8355-79cc0a720ea0</NS1:BatchID>
      <NS1:ChangeStatus>NA</NS1:ChangeStatus>
    </NS1:Price>
  </NS1:ProductPrice>
</NS1:PriceMaintenanceRequest>

I need to remove all nodes with BusinessUnitId="90". So in this example, I end up with this:

<NS1:PriceMaintenanceRequest xmlns:NS1="http://retalix.com/R10/services" MajorVersion="2" MinorVersion="0" FixVersion="0">
  <NS1:Header>
    <NS1:MessageId>1234</NS1:MessageId>
    <NS1:Bulk UnitOfWork="false"/>
  </NS1:Header>
  <NS1:ProductPrice BusinessUnitId="9200" Action="AddOrUpdate">
    <NS1:ProductId>2840000133</NS1:ProductId>
    <NS1:Price Action="AddUpdate" Sequence="1234">
      <NS1:UnitOfMeasureCode>EA</NS1:UnitOfMeasureCode>
      <NS1:EffectiveDateTimestamp>2012-09-20T00:00:00</NS1:EffectiveDateTimestamp>
      <NS1:EndDateTimestamp>2056-12-12T23:59:00</NS1:EndDateTimestamp>
      <NS1:CatalogPrice Currency="USD">4.2900</NS1:CatalogPrice>
      <NS1:BatchID>491364c5-73f5-45a4-8355-79cc0a720ea0</NS1:BatchID>
      <NS1:ChangeStatus>NA</NS1:ChangeStatus>
    </NS1:Price>
  </NS1:ProductPrice>
</NS1:PriceMaintenanceRequest>

How do i do it with PowerShell?
I need to load a files containing this xml from a directory, manipulate them, and save them.

Thanks a lot

Upvotes: 0

Views: 111

Answers (1)

Frode F.
Frode F.

Reputation: 54881

You can do something like this:

Get-ChildItem -Path "C:\FolderWithXMLs" -Filter "*.xml" | ForEach-Object {

    $path = $_.FullName
    $xml = [xml](Get-Content $path)

    $xml.PriceMaintenanceRequest.ProductPrice | ? { $_.BusinessUnitId -eq "90" } | % { 
        #Remove node
        $xml.PriceMaintenanceRequest.RemoveChild($_)
    }

    $xml.Save($path)

}

Upvotes: 2

Related Questions