Edit XML with Powershell - merge / switch nodes

bjdriver 1 Reputation point
2020-09-28T05:58:52.513+00:00

I have a XML like this below and want to create a script, which reads all tracking numbers, maybe in an array, save them. Then the script should delete all <TrackingNumber>- lines, insert only one line <TrackingNumber> in section <TrackingNumbers>, which contains the saved tracking numbers from the array. Can anybody help me here ?<?xml version="1.0" encoding="utf-8" standalone="yes"?>

<ShipmentInformation>
<Document No="S006122">
<ShipmentDate>2020-07-30T00:00:00</ShipmentDate>
<LanguageCode />
<CustomerInformation>
<ExternalDocumentNo>4501779882</ExternalDocumentNo>
</CustomerInformation>
<InvoiceAddress>
<CustomerNo>138321110</CustomerNo>
<ContactNo>KT435379</ContactNo>
<CustomerName>Name1</CustomerName>
<Address>Street2</Address>
<PostCode>12345</PostCode>
<City>City1</City>
<CountryCode>DE</CountryCode>
</InvoiceAddress>
<ShipmentAddress>
<CustomerName>Nam2</CustomerName>
<Address>Mr.Bond</Address>
<Address2>Street2</Address2>
<PostCode>54321</PostCode>
<City>City2</City>
<CountryCode>DE</CountryCode>
</ShipmentAddress>
<TrackingNumbers>
<ShippingAgent>DHL</ShippingAgent>
<TrackingNumber>1Z5686016811115412</TrackingNumber>
<TrackingNumber>1Z5686016811115226</TrackingNumber>
<TrackingNumber>1Z5686016811112635</TrackingNumber>
<TrackingNumber>1Z5686016811113640</TrackingNumber>
<TrackingNumber>1Z5686016811114250</TrackingNumber>
<TrackingNumber>1Z5686016811110460</TrackingNumber>
</TrackingNumbers>
</Document>
</ShipmentInformation>

Result should be:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<ShipmentInformation>
<Document No="S006122">
<ShipmentDate>2020-07-30T00:00:00</ShipmentDate>
<LanguageCode />
<CustomerInformation>
<ExternalDocumentNo>4501779882</ExternalDocumentNo>
</CustomerInformation>
<InvoiceAddress>
<CustomerNo>138321110</CustomerNo>
<ContactNo>KT435379</ContactNo>
<CustomerName>Name1</CustomerName>
<Address>Street2</Address>
<PostCode>12345</PostCode>
<City>City1</City>
<CountryCode>DE</CountryCode>
</InvoiceAddress>
<ShipmentAddress>
<CustomerName>Nam2</CustomerName>
<Address>Mr.Bond</Address>
<Address2>Street2</Address2>
<PostCode>54321</PostCode>
<City>City2</City>
<CountryCode>DE</CountryCode>
</ShipmentAddress>
<TrackingNumbers>
<ShippingAgent>DHL</ShippingAgent> <TrackingNumber>1Z5686016811115412,1Z5686016811115226,1Z5686016811112635,1Z5686016811113640,1Z5686016811114250,1Z5686016811110460</TrackingNumber>
</TrackingNumbers>
</Document>
</ShipmentInformation>

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2020-09-28T09:45:00.497+00:00

    Hi,
    You could use RemoveChild to delete nodes and AppendChild to add new nodes. Please check if this works for you

    $FilePath = "C:\Folder\File.xml"  
    [xml]$xml = Get-Content $FilePath  
    $TrackingNumber = @()  
    $xml.SelectNodes("//TrackingNumber") | ForEach-Object{  
        $TrackingNumber += $_.'#text'  
        $_.ParentNode.RemoveChild($_)  
    }  
    $ChildElement = $xml.CreateElement("TrackingNumber")  
    $ChildElementText = $xml.CreateTextNode($TrackingNumber)  
    $ChildElement.AppendChild($ChildElementText)  
    $xml.SelectNodes("//TrackingNumbers").Appendchild($ChildElement)  
    $xml.Save($FilePath)  
    

    Best Regards,
    Ian

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    2 people found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.