1

I have a little problem with my XML and PowerShell script. I need remove a Vlan Item of the XML file:

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

But I can't remove <Vlan>Vlan4$</Vlan> with the RemoveChild method....

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

Can you help me please ?

1 Answer 1

1

You have to call the RemoveChild method on the parent and pass the actual node you want to delete as parameter:

$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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.