Search code examples
xmlwix

WiX XML Always modifies the first element in my .config, ignoring the key property


I am trying to understand Wix, but I can't properly modify my .config file. Here is an example:

<configuration>
 <configSections>
<!--All custom Sections must be delcared here-->
   <section name="settings" type="System.Configuration.AppSettingsSection"/>
 </configSections>
   <settings>
    <add key="DatabaseServer" value="localhost"/>
    <add key="DatabaseName" value="SampleDB"/>
  </settings>
</configuration>

And I want to modify DatabaseName in my wix project like this:

<Component Id="ModifyConfig" >
<File Id="AppConfigFile" Source="..path\to\my.config" KeyPath="yes" />
<util:XmlFile Id="SetAppSetting"
              File="[#AppConfigFile]"
              Action="setValue"
              ElementPath="/configuration/settings/add[key='DatabaseName']"
              Name="value"
              Value="somethingElse" />
</Component>

Also tried with @key. However, I only manage to modify the first value (DatabaseServer). I am pretty sure it has something to do with the key property not being properly resolved, yet chatGPT and a quick google search show me no different approach than what I've already been doing.

How do i modify the DatabaseName instead?


Solution

  • You need to escape square brackets (and use @key):

    <util:XmlFile
       ElementPath="/configuration/settings/add[\[]@key='DatabaseName'[\]]"
    />
    

    The problem with unescaped [xxx] is that these fragments are pre-processed (see Formatted) and replaced by MSI before being passed to the XmlFile action. So in your case what is passed to the extension is /configuration/settings/add (resolves to the first element)