Thibaultc Thibaultc - 3 months ago 32
PowerShell Question

modify item innerXml with powershell

So I've a little with my PowerShell script and XML file.
Currently the XML file is formated like this :

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


And my Powershell script like this :

function modifyXmlItem ($selectedItem, $newValue) {

$node = $xml.Settings.ARP.configVLAN | where {$_ -eq $selectedItem}
$node = $newValue
$xml.Save($path)
}


So I want change value of the Vlan1 by a value from a textbox.
I can get value form the textbox, I can get value selected (Vlan 1), but I failed to change the "Vlan1"...

But this function doesn't work ...
I have tried many solution provided from the web but none working...

Can you help me please...
(Sorry for my bad English..)

Answer

Try this:

$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>'
$selectedItem = 'Vlan4$'

$xml.Settings.ARP.ConfigVLAN.SelectSingleNode("Vlan[text()=""$($selectedItem)""]").InnerText = "NewValue"
$xml.Save('d:\temp\out.xml')
Comments