Note that there are some explanatory texts on larger screens.

plurals
  1. POadding XML sub-elements
    primarykey
    data
    text
    <p>With PowerShell, I want to add several sub-elements into an XML tree.<br> I know to ADD ONE element, I know to add one or several attributes, but I don't understand how to ADD SEVERAL elements.</p> <p>One way whould be to <a href="https://stackoverflow.com/questions/10524389/powershell-inserting-multiple-xml-elements">write a sub-XML tree as text</a><br> But I can't use this method because the elements are not added at once.</p> <p>To add one element, I do that:</p> <pre><code>[xml]$xml = get-content $nomfichier $newEl = $xml.CreateElement('my_element') [void]$xml.root.AppendChild($newEl) </code></pre> <p>Works fine. This give me this XML tree:</p> <pre><code>$xml | fc class XmlDocument { root = class XmlElement { datas = class XmlElement { array1 = [ value1 value2 value3 ] } my_element = &lt;-- the element I just added } } </code></pre> <p>Now I want to add a sub element to 'my_element'. I use a similar method: </p> <pre><code>$anotherEl = $xml.CreateElement('my_sub_element') [void]$xml.root.my_element.AppendChild($anotherEl) &lt;-- error because $xml.root.my_element is a string [void]$newEl.AppendChild($anotherEl) &lt;-- ok $again = $xml.CreateElement('another_one') [void]$newEl.AppendChild($again) </code></pre> <p>This give this XML tree (partialy displayed):</p> <pre><code>my_element = class XmlElement { my_sub_element = another_one = } </code></pre> <p>Those are attributes, not sub-elements.<br> Sub-elements would be displayed as this: </p> <pre><code>my_element = [ my_sub_element another_one ] </code></pre> <p><strong>Question</strong>: How do I add several sub-elements, one at a time?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload