0

I have this XML file: environment-config.xml

<?xml version="1.0" encoding="windows-1252"?>
<environment>uat</environment>

I would like to update the value and have this script.

[string]$EnvConfigFileName = ".\environment-config.xml"
$EnvironmentName = 'prod'

[XML]$xmlFileContent = Get-Content $EnvConfigFileName

$xmlNode = $xmlFileContent.SelectSingleNode("/environment")
$xmlNode.Value = $EnvironmentName

$xmlFileContent.Save($EnvConfigFileName)

On execution it generates this error: Exception setting "Value": "Cannot set a value on node type 'Element'." ---> System.InvalidOperationException: Cannot set a value on node type 'Element'.

Is there another way to do this?

Thanks in advance.

Bertus
  • 3
  • 4

1 Answers1

0

You can get the nodes as shown in below code and change the value.

[xml]$xmlData = Get-Content $EnvConfigFileName 
$xmlData.environment = "Data"

#Save the file
$xmlData.Save($EnvConfigFileName)
Narendra Sharma
  • 549
  • 4
  • 15