Need help with changing an element value in below XML using PowerShell:
<?xml version="1.0" encoding="ISO-8859-1"?>
<Root>
<Package>
<ID>001</ID>
<Quantity1>65465</Quantity1>
<Quantity2>CALC</Quantity2>
</Package>
<Package>
<ID>002</ID>
<Quantity1></Quantity1>
<Quantity2>1547625</Quantity2>
</Package>
</Root>
I need to loop through the XML file and
cut value from
<Quantity1>and paste it to<Quantity2>in the first case (ID:001).cut value from
<Quantity2>, paste it to<Quantity1>and make<Quantity2>equals to 'CALC' in the second case (ID:002).
I've tried this code:
[XML]$XMLcontents = [XML](Get-Content $PathToTheFile)
foreach ($i in $XMLcontents.SelectNodes('/Root/Package')) {
$_.Quantity2 = $_.Quantity1
}
but I'm getting the following error:
The property 'Quantity2' cannot be found on this object.
How to properly reference elements within a node <Package>?