使用PowerShell删除特定的xml元素

时间:2014-04-06 14:57:49

标签: powershell

我有以下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>

我需要删除BusinessUnitId =“90”的所有节点。所以在这个例子中,我最终得到了这个:

<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>

我如何使用PowerShell?
我需要从目录中加载包含此xml的文件,对其进行操作并保存它们。

非常感谢

1 个答案:

答案 0 :(得分:2)

您可以这样做:

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)

}