PowerShell:如何将XmlElement添加到非根元素

PowerShell:如何将XmlElement添加到非根元素,xml,powershell,Xml,Powershell,在PowerShell中将XmlElement添加到非根元素时遇到问题 基本上,考虑到这种xml: <clubs> <club name="boca" position="1"> <field>bombonera</field> <field>bombonerita</field> </club> &l

在PowerShell中将XmlElement添加到非根元素时遇到问题

基本上,考虑到这种xml:

<clubs>
        <club name="boca" position="1">
                <field>bombonera</field>
                <field>bombonerita</field>
        </club>
        <club name="racing" position="19">
                <field>cilindro</field>
        </club>
</clubs>
当我尝试将此元素添加到非根节点时,即

$clubs.clubs.club += $new
我明白了


我缺少什么?

尝试在适当的元素上使用AppendChild方法。AppendChild还有一些替代方法,如中所述,允许您更好地控制DOM树中的位置

$club = $xml.CreateElement('club')
$club.SetAttribute('name','barracas')
$xml.clubs.AppendChild($club)
$clubs.clubs.club += $new
Cannot set "club" because only strings can be used as values to set XmlNode properties.
$club = $xml.CreateElement('club')
$club.SetAttribute('name','barracas')
$xml.clubs.AppendChild($club)