2016-08-23 140 views
1

我的XML和PowerShell脚本有点问题。 我需要删除Vlan项目的XML文件:根据节点值删除XML节点

<?xml version="1.0" encoding="utf-8"?> 
<Settings> 
    <ARP> 
    <ConfigVLAN> 
     <Vlan>Vlan1</Vlan> 
     <Vlan>Vlan2</Vlan> 
     <Vlan>Vlan3</Vlan> 
     <Vlan>Vlan4$</Vlan> 
    </ConfigVLAN> 
    </ARP> 
</Settings> 

但我不能删除<Vlan>Vlan4$</Vlan>RemoveChild方法....

$xml.Settings.ARP.configVLAN.SelectSingleNode("Vlan[text()=""$($selectedItem)""]").RemoveChild() 

你能帮助我吗?

回答

1

你必须调用父的RemoveChild方法,并传递要删除的参数实际节点:

$selectedItem = 'Vlan4$' 
$xmlFilePath = "Your_xml_file_path" 

$xml = [xml](Get-Content $xmlFilePath) 
$nodeToRemove = $xml.Settings.ARP.configVLAN.SelectSingleNode("Vlan[text()=""$($selectedItem)""]") 
$xml.Settings.ARP.ConfigVLAN.RemoveChild($nodeToRemove) | out-null 
$xml.Save($xmlFilePath)