We help IT Professionals succeed at work.

Change specific values in .xml file

Omair Ahmed
Omair Ahmed asked
on
I had this question after viewing Change a specific key value in app.config file with another value..


I have a very similar issue but the solution provided to the question above is not working for me. The .xml file that I want to update has entries like the ones below. I need to update some of the entries not all. The values that I will provide need to be stored as variables, not hard coded.


<?xml version="1.0" encoding="utf-8"?>
<parameters>
  <setParameter name= "MachineValidationKey" value="" />
  <setParameter name= "Smart Card Auth URI" value="https://localhost/SmartCardAuthService/" />
  <setParameter name= "MachineDecryptionKey" value="" />
</parameters>


My code (based on the previous answer) is below. Is the "add[@key]" specific to the config file or is that a construct for the SelectSingleNode?  


$filename = 'server1.xml'

$XPath = "parameters/setParameter/add[@key='MachineValidationKey']"

#$XPath = "parameters/setParameter/add[@name='MachineValidationKey']"

$Attribute = "value"
$machinekey = 'SecretString123'

$xml = [xml] (Get-Content -Path $filename)

#$xml.SelectSingleNode($XPath).SetAttribute($Attribute,$machinekey)

$xml.SelectSingleNode("parameters/setParameter/add[@name='MachineValidationKey']").SetAttribute("value", "$machinekey")

$xml.Save($filename)


But is resulting in the following error message:


You cannot call a method on a null-valued expression.
At C:\data\AFNET_Installation\scripts\xml-test.ps1:15 char:1
+ $xml.SelectSingleNode("parameters/setParameter/add[@name='MachineVali ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018

Commented:
The "add" is a node name (that does not exist in your XML), and the @name is the attribute you're filtering for.
Try it like this:
$filename = 'server1.xml'

$xPath = "parameters/setParameter[@name='MachineValidationKey']"
$attribute = "value"
$machinekey = 'SecretString123'

$xml = [xml] (Get-Content -Path $filename)
$xml.SelectSingleNode($xPath).SetAttribute($attribute, $machinekey)

$xml.Save($filename)

Open in new window

Omair AhmedBuild Engineer

Author

Commented:
Thank you for a quick response. Please see error message below:

Exception calling "SelectSingleNode" with "1" argument(s): "Expression must evaluate to a node-set."
At C:\data\AFNET_Installation\scripts\xml-test.ps1:7 char:1
+ $xml.SelectSingleNode($xPath).SetAttribute($attribute, $machinekey)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

My script based on your feedback:




$filename = 'server1.xml'
$xPath = "parameters/setParameter/[@name='MachineValidationKey']"
$attribute = "value"
$machinekey = "SecretString123"
$xml = [xml] (Get-Content -Path $filename)
$xml.SelectSingleNode($xPath).SetAttribute($attribute, $machinekey)
$xml.Save($filename)
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018
Commented:
That is not what I posted.
My post:
$xPath = "parameters/setParameter[@name='MachineValidationKey']"
Your version:
$xPath = "parameters/setParameter/[@name='MachineValidationKey']"
You inserted a slash between "setParameter" and "[@"
Omair AhmedBuild Engineer

Author

Commented:
Thank you. This worked perfectly!