0

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

1 Answer 1

2

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)

}
Sign up to request clarification or add additional context in comments.

2 Comments

Works great, but I forgot to to add that i need to do it for all files in a directory... sorry.
Fixed. Show some effort next time. Searching for files and running a foreach-loop is something everyone should be able to do. This isn' a webstore for powershell solutions. We actually expect you to be able to tweak it yourself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.