Powershell weirdly encodes ampersands when I try to set a value to an xml attribute

In my build script, I have a powershell helper function, as shown below:

function set-connectionstring {
  param($path, $name, $value)
  $settings = [xml](get-content $path)
  $setting = $settings.configuration.connectionStrings.add | where { $_.name -eq $name }
  $setting.connectionString = "$value"
  $setting.providerName = "System.Data.SqlClient"
  $resolvedPath = resolve-path($path) 
  $settings.save($resolvedPath)
}

It works fine, except that when I try to set a value, it encodes ampersands strangely. Here is the problem:

My connection string has a value ". That should be perfectly fair. However, my code above converts this to ", which is completely unreasonable. Do you have an idea, can I solve this problem?

+5
source share
2 answers

Have you tried the decodeconnection string? Here is an example:

[System.Reflection.Assembly]::LoadWithPartialName("System.web")
[System.Web.HttpUtility]::HtmlDecode(""")
+5
source

$value , , : $setting.connectionString = ""something"". XML, .NET, , , , , XML. , unescaped, .

+1

All Articles