Adding an XML element only if it does not already exist

In this recipe, we'll learn a technique of adding an XML element only if it isn't already present. When updating a configuration file that's shared amongst applications, such as the machine-level Web.config file, you might use this to keep old user settings intact or else create them anew if they aren't already set. For this recipe, we'll check for the existence of an element called add with a key attribute of UserSetting1 and add it if it isn't already there.

Getting ready

To prepare for this recipe, perform the following steps:

  1. Create a new setup project and call it XmlElementExistsInstaller.
  2. Add an XML file called Web.config with the following markup:
    <?xml version="1.0"?> <configuration> <appSettings> ...

Get WiX Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.