Сначала необходимо захватить узлы «ConsignmentNumber» и «ConsignmentHeader», удалить узел «Consignment» из xml и восстановить его.
Для демонстрации я использую Here-String , но вам, вероятно, потребуется прочитать его из файла, используя [xml]$xml = Get-Content -Path 'D:\MyCurrentXml.xml'
[xml]$xml = @"
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<Shipment xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" >
<ShipmentNumber>0012546776</ShipmentNumber>
<Container>Selleys/Yates</Container>
<Extnl_id/>
<AddressForwardingAgent>
<PartnerNumber>0000400260</PartnerNumber>
<LanguageKey>EN</LanguageKey>
<Name>TEST</Name>
<HouseandStreet>TEST1</HouseandStreet>
</AddressForwardingAgent>
<AddressSender>
<TransportPlanningPoint>4001</TransportPlanningPoint>
<LanguageKey>EN</LanguageKey>
<Name>TEST2</Name>
<HouseandStreet>TEST2</HouseandStreet>
</AddressSender>
<Consignment>
<ConsignmentNumber>0001</ConsignmentNumber>
<ConsignmentHeader>
<DangerousGoodsIndicator>X</DangerousGoodsIndicator>
<GrossWeight>2005.268</GrossWeight>
<WeightUOM>KG</WeightUOM>
<TotalVolume>3.856</TotalVolume>
<VolumeUOM>M3</VolumeUOM>
</ConsignmentHeader>
<ConsignmentNumber>0002</ConsignmentNumber>
<ConsignmentHeader>
<DangerousGoodsIndicator>X</DangerousGoodsIndicator>
<GrossWeight>21.12</GrossWeight>
<WeightUOM>KG</WeightUOM>
<TotalVolume>0.044</TotalVolume>
<VolumeUOM>M3</VolumeUOM>
</ConsignmentHeader>
<ConsignmentNumber>0003</ConsignmentNumber>
<ConsignmentHeader>
<DangerousGoodsIndicator>Y</DangerousGoodsIndicator>
<GrossWeight>12.45</GrossWeight>
<WeightUOM>KG</WeightUOM>
<TotalVolume>0.056</TotalVolume>
<VolumeUOM>P3</VolumeUOM>
</ConsignmentHeader>
</Consignment>
</Shipment>
"@
Чтобы восстановить его так, как вы хотите, это должно работать:
# capture two arrays of nodes for 'ConsignmentNumber' and 'ConsignmentHeader'
$numbers = $xml.Shipment.Consignment.ChildNodes | Where-Object { $_.LocalName -eq 'ConsignmentNumber' }
$headers = $xml.Shipment.Consignment.ChildNodes | Where-Object { $_.LocalName -eq 'ConsignmentHeader' }
# remove the entire 'Consignment' node from the xml
$xml.Shipment.RemoveChild($xml.Shipment.Consignment)
# and create a new 'Consignment' node
$newConsignment = $xml.CreateElement('Consignment')
# loop through the arrays, create a new 'ConsigmentLine' node and add the captured nodes to it
for ($i = 0; $i -lt $numbers.Count; $i++) {
$lineNode = $xml.CreateElement('ConsigmentLine')
$lineNode.AppendChild($numbers[$i])
$lineNode.AppendChild($headers[$i])
# append this to the newly created 'Consignment' node
$newConsignment.AppendChild($lineNode)
}
# finally, append the new 'Consignment' node to the 'Shipment' node
$xml.Shipment.AppendChild($newConsignment)
# and save the xml
$xml.Save('D:\MyCorrectedXml.xml')
Результат:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Shipment xmlns:xsi="http://www.w3.org/2001/xmlschema-instance">
<ShipmentNumber>0012546776</ShipmentNumber>
<Container>Selleys/Yates</Container>
<Extnl_id />
<AddressForwardingAgent>
<PartnerNumber>0000400260</PartnerNumber>
<LanguageKey>EN</LanguageKey>
<Name>TEST</Name>
<HouseandStreet>TEST1</HouseandStreet>
</AddressForwardingAgent>
<AddressSender>
<TransportPlanningPoint>4001</TransportPlanningPoint>
<LanguageKey>EN</LanguageKey>
<Name>TEST2</Name>
<HouseandStreet>TEST2</HouseandStreet>
</AddressSender>
<Consignment>
<ConsigmentLine>
<ConsignmentNumber>0001</ConsignmentNumber>
<ConsignmentHeader>
<DangerousGoodsIndicator>X</DangerousGoodsIndicator>
<GrossWeight>2005.268</GrossWeight>
<WeightUOM>KG</WeightUOM>
<TotalVolume>3.856</TotalVolume>
<VolumeUOM>M3</VolumeUOM>
</ConsignmentHeader>
</ConsigmentLine>
<ConsigmentLine>
<ConsignmentNumber>0002</ConsignmentNumber>
<ConsignmentHeader>
<DangerousGoodsIndicator>X</DangerousGoodsIndicator>
<GrossWeight>21.12</GrossWeight>
<WeightUOM>KG</WeightUOM>
<TotalVolume>0.044</TotalVolume>
<VolumeUOM>M3</VolumeUOM>
</ConsignmentHeader>
</ConsigmentLine>
<ConsigmentLine>
<ConsignmentNumber>0003</ConsignmentNumber>
<ConsignmentHeader>
<DangerousGoodsIndicator>Y</DangerousGoodsIndicator>
<GrossWeight>12.45</GrossWeight>
<WeightUOM>KG</WeightUOM>
<TotalVolume>0.056</TotalVolume>
<VolumeUOM>P3</VolumeUOM>
</ConsignmentHeader>
</ConsigmentLine>
</Consignment>
</Shipment>
PS в вашем примере xml отсутствует окончательное закрытие </Shipment>
тег