I have an xml file with a number of properties
<configuration>
<property>
<name>access.key</name>
<value>fred</value>
</property>
<property>
<name>access.secret</name>
<value>blog</value>
</property>
</configuration>
I want to replace the property value based on the property name. I've tried a number of ways. I'm having issues because name and value are elements of property and not attributes. I had though this would work but no luck.
$file = "site.xml"
$xml = New-Object XML
$xml.Load($file)
$nodes = $xml.SelectNodes('/configuration/property')
foreach ($node in $nodes) {
$property = $node.Attributes.GetNamedItem("name").Value
if ($property -eq 'access.key')
{
$node.Attributes.SetNamedItem("value").Value = '{{ access_key }}'
}
}
$xml.Save($file)
The following changed, but it changed value of name which makes sense because I selected the SingleNode property name. How do I change the property value base on the name?
$node = $xml.SelectSingleNode("/configuration/property/name[.= 'access.key']")
$node.innerText = '{{ access_key }}'
It seems simple and probably is, hope someone can shed some light.