开发者

Powershell Add XmlElement As the first child

开发者 https://www.devze.com 2023-03-13 04:33 出处:网络
This is a variation of the following question at: PowerShell: How to add XmlElement to a non-root element

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

0

精彩评论

暂无评论...
验证码 换一张
取 消