Powershell Add XmlElement As the first child
This is a variation of the following question at:
PowerShell: How to add XmlElement to a non-root element
So I'll run with the data the OP used in that question. Given the following XML snippet:
<clu开发者_如何学JAVAbs>
</clubs>
or
<clubs />
What I'm trying to do is add the first element so that my resulting XML looks like:
<clubs>
<club name="barracas" rating="awesome" />
</clubs>
So far, I have tried: Append, InsertAfter (although I'm not 100% sure how that works)
$newNode = $xml.CreateElement("club")
$newNode.SetAttribute("name", "barracas");
$newnode.SetAttribute("rating", "awesome");
$xml.clubs.Append($newnode)
$xml.clubs.InsertAfter($newNode, $xml.clubs)
$xml.clubs
is a string in this scenario and not an XmlNode
. Try this instead:
$xml.FirstChild.AppendChild($newNode)
Of course, if the element is further down the tree, you'd probably be better off using the SelectSingleNode()
method
精彩评论