0

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.

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Palle, almost there! This created 3 new elements but the data is inside the element like this '<WebS xmlns="00001" /> as opposed to <WebS>"00001"</WebS>. Is this something to do with OuterXML, InnerXml ?
ok I have it. I just needed an extra line with the content of the element which was $elem.InnerXml = '00001'. Thanks Palle!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.