[xml].Save is changing characters for the value of a certain key


I’ve got an XML file that contains some settings for a an application. The value of the trackingScheduler key is a string that contains JSON. I’ve included an example of the JSON below.

<add key="trackingSchedulers" value="[ { 'Name':'wowTracking_1', 'Available':false, 'MaxAllowed':20000 }, { 'Name':'wowTracking_2', 'Available':true, 'MaxAllowed':1000 } ]"/> 

The issue that I am having is that when I call $xml.save I get substituted characters in the JSON string that’s written back to the file like so: value=” [ {
‘Name’:’wowTracking_1′, ‘Available’:false, ‘MaxAllowed’:20000,

$path = "C:Infrastructure-ScriptsPowershellScriptsTestFilesappsettings.config.new" [xml]$XmlDocument = Get-Content -Path ..TestFilesappsettings.config -Raw $nodes = $XmlDocument.SelectNodes("/appSettings/add[@key]") foreach ($node in $nodes) { if ($node.attributes[0].value -eq "trackingSchedulers") { $JSON = $node.value | ConvertFrom-Json Write-Host "$JSON" foreach ($JSONobject in $JSON) { if ($JSONobject.Name -eq $LowestQueue) { $JSONobject.Available = "true" } if ($JSONobject.Name -eq $servicedisplayname) { $JSONobject.Available = "false" } } $JSONstring = $JSON | ConvertTo-Json $JSONstring = $JSONstring.Replace("`r","") $JSONstring = $JSONstring.Replace("`n","") $node.Attributes[1].FirstChild.InnerText = $JSONstring $XmlDocument.Save($path) } 


Any insight into how I can get around this issue would be greatly appreciated.

submitted by /u/avestuk
[link] [comments]

Leave a Reply