基于节点值删除XML节点

基于节点值删除XML节点,xml,powershell,Xml,Powershell,我的XML和PowerShell脚本有点问题。 我需要删除XML文件的Vlan项: <?xml version="1.0" encoding="utf-8"?> <Settings> <ARP> <ConfigVLAN> <Vlan>Vlan1</Vlan> <Vlan>Vlan2</Vlan> <Vlan>Vlan3</Vlan&g

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

<?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>

您能帮我吗?

您必须调用父节点上的
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)
$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)