Question:
I would like to insert a node between 2 others already existing.
In my script, I receive a xml variable and I would like to update this one.
Ex :
1 2 3 4 5 6 7 |
the result should be :
1 2 3 4 5 6 7 8 |
When I use a appendChild, the insert is done always done at the end…
An idea ?
Thanks !
Answer:
As @Grhm answered, you can do it by InsertAfter
. I would recommend always to try to pipe it to Get-Member
to get the hint.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
> $x = [xml]@" "@ > $x | gm -membertype method TypeName: System.Xml.XmlDocument Name MemberType Definition ---- ---------- ---------- AppendChild Method System.Xml.XmlNode AppendChild(System.Xml .. ImportNode Method System.Xml.XmlNode ImportNode(System.Xml. InsertAfter Method System.Xml.XmlNode InsertAfter(System.Xml InsertBefore Method System.Xml.XmlNode InsertBefore(System.Xm Load Method System.Void Load(string filename), System ... WriteTo Method System.Void WriteTo(System.Xml.XmlWriter > $newe = $x.CreateElement('newelement') > $x.mapping.InsertAfter($newe, $x.mapping.INSTANCE[1]) > $x | Format-Custom |
Personally I think gm
(or Get-Member
) is the most useful cmdlet in PowerShell 😉