How to use WiX to congiure XML files during installation.

One of WiX's powerful features is XML interaction. WiX can create and delete elements or change values and attributes inside XML files. To do this we use either the util:XmlFile or util:XmlConfig elements. The two elements differ in subtle ways. util:XmlFile does not let you specify to be run on uninstall though it does allow you set the Permanent flag, which will undo the action on uninstall, while util:XmlConfig only allows you to delete or create an element, not update one.

To use either you must include a reference to WixUtilExtension.dll and add the namespace as follows:

Both elements contain the action attribute. This specifies the type of XML modification to be made. For util:XmlFile:

  • createElement
  • deleteValue
  • setValue
  • bulkSetValue

util:XmlFile does not have setValue or bulkSetValue. setValue matches the first element and updates its value while bulkSetValue updates all elements that are matched.

The other important attribute is ElementPath. This is an XPath which is used for matching the element (or for util:XmlFile elements) that needs to be updated. For more information on XPath view this tutorial. I also recommend this XPath evaluator for testing your expressions.

So lets have a look at an example. In this case we will alter a app.config file using util:XmlFile. This is an app.config file that we are installing into the default installation directory.

What is happening here? Firstly the ElementPath XPath is set to match <configuration><connectionStrings><add connectionString = "" ⁄>. Secondly because the Name attribute is set it will set the value of the attribute rather then the elements inner value. If and when this node is matched the Value will be set to populate the attribute. Note that this util:XmlFile is nested inside the same component that contains the config file, it does however execute after the file is installed.