Powershell: adding XML sub-elements

SukhjitSh4x0r

New Member
With PowerShell, I want to add several sub-elements into an XML tree.
I know to ADD ONE element, I know to add one or several attributes, but I don't understand how to ADD SEVERAL elements.One way whould be to write a sub-XML tree as text
But I can't use this method because the elements are not added at once.To add one element, I do that:\[code\][xml]$xml = get-content $nomfichier$newEl = $xml.CreateElement('my_element')[void]$xml.root.AppendChild($newEl)\[/code\]Works fine. This give me this XML tree:\[code\]$xml | fcclass XmlDocument{ root = class XmlElement { datas = class XmlElement { array1 = [ value1 value2 value3 ] } my_element = <-- the element I just added }}\[/code\]Now I want to add a sub element to 'my_element'. I use a similar method:
\[code\]$anotherEl = $xml.CreateElement('my_sub_element')\[/code\]
\[code\][void]$xml.root.my_element.AppendChild($anotherEl) <-- error because $xml.root.my_element is a string\[/code\]
\[code\][void]$newEl.AppendChild($anotherEl) <-- ok\[/code\]
\[code\]$again = $xml.CreateElement('another_one')\[/code\]
\[code\][void]$newEl.AppendChild($again)\[/code\]This give this XML tree (partialy displayed):\[code\]my_element = class XmlElement { my_sub_element = another_one = }\[/code\]Those are attributes, not sub-elements.
Sub-elements would be displayed as this: \[code\]my_element = [ my_sub_element another_one ]\[/code\]Question: How do I add several sub-elements, one at a time ?
 
Back
Top