5

In my build script, I have a helper powershell function as 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 great except that when I try to set the value, it encodes ampersands weirdly. Here is the issue:

Inside my connection string I have &quot value. This should be perfectly valid. However, my above code transforms this into " which is totally unreasonable. Do you have any idea I can solve this problem?

tugberk
  • 57,477
  • 67
  • 243
  • 335

2 Answers2

6

Have you try to decode your connection string first? Here is sample:

[System.Reflection.Assembly]::LoadWithPartialName("System.web")
[System.Web.HttpUtility]::HtmlDecode(""")
Akim
  • 8,469
  • 2
  • 31
  • 49
1

You are sticking $value in the middle of a string literal, so it is behaving as though you had typed: $setting.connectionString = ""something"". This is not an XML operation, it is a .NET string assignment, so anything on the right hand side of the equal needs to be literally what you want in the connection string, not the escaped XML attribute representation. So either pass it in unescaped, or unescape it before the assignment.

jlew
  • 10,491
  • 1
  • 35
  • 58
  • it doesn't matter. if I remove the quotes, the same happens again. – tugberk Jul 23 '12 at 12:11
  • I know, the point is that your string contains encoded XML entities and is not being treated as an XML string (which is why the HtmlDecode works) – jlew Jul 23 '12 at 19:15