Reputation: 325
I have a script that reads from an XML file, performs some web services, from which it gets a response and I'd now like the XML fie to be amended with the webs response as below.
Original XML:
<ItemNum>
<Key>11233</Key>
<Item>123.jpg</Item>
<Item>456.jpg</Item>
<Item>789.jpg</Item>
<Detail>Processed on 8-12-2017</Detail>
</ItemNum>
The script will process the items from the XML and iterate through the itemlist, similar to below:
[xml]$xml = Get-Content -Path $xmlPath
Foreach ($websResponse in $websResponseList) {
<Create New Element>
<Add Webs Response to Element>
}
$xml.save($xmlPath)
The result should be the original XML modified to look like the below:
<ItemNum>
<Key>11233</Key>
<Item>123.jpg</Item>
<Item>456.jpg</Item>
<Item>789.jpg</Item>
<Detail>Processed on 8-12-2017</Detail>
<WebS>00001</WebS>
<WebS>00002</WebS>
<WebS>00003</WebS>
</ItemNum>
I've read countless articles on working with XML in Powershell and probably some that are very similar what I'm trying to achieve but this is for some reason stumping me so would appreciate any pointers.
Upvotes: 0
Views: 220
Reputation: 6312
You're almost there:
[xml]$xml = Get-Content -Path $xmlPath
foreach ($websResponse in $websResponseList)
{
$elem = $xml.CreateElement('WebS','00001') # substitute with data
$xml.ItemNum.AppendChild($elem)
}
$xml.save($xmlPath)
Upvotes: 1