You’re basically on the right track. All calls to CreateElement() (and similar methods) will be made on your $PY variable, but that doesn’t mean that the elements will be placed at the document root. What matters is which node you call AppendChild() on. I can’t see your XML attachment (it would need to be renamed to .txt), but I still have a copy of an XML sample that you attached to your other thread. In that file, your PAYLINE nodes were all placed under LOAD_PAYABLE_005\DATAAREA\LOAD_PAYABLE , so I’ll assume that’s where you want to put the new payline node in this code. Here’s now that would work:
$loadPayableNode = $PY.SelectSingleNode('/LOAD_PAYABLE_005/DATAAREA/LOAD_PAYABLE') $newpayline = $PY.CreateElement('PAYLINE') $null = $loadPayableNode.AppendChild($newpayline) $newelement = $PY.CreateElement('ORIGREF') $newelement.InnerText = $InvOrigRef.Node.InnerText.Trim() $null = $newpayline.AppendChild($newelement)
On a side note, notice that I used the InnerText property instead of calling the set_InnerText method on $newelement. That’s personal preference, I suppose, but most C# and PowerShell code will make use of the Property syntax rather than calling the get_ and set_ methods directly.
I also assigned the results of AppendChild() to $null, to prevent it from polluting the pipeline (or your screen.) Those methods return a reference to the element that was appended, allowing you to chain them together. For example, you could have created the two elements and then inserted them like this:
$null = $loadPayableNode.AppendChild($newpayline).AppendChild($newelement)