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:
Create a new setup project and call it
XmlElementExistsInstaller
.Add an XML file called
Web.config
with the following markup:<?xml version="1.0"?> <configuration> <appSettings> <add key="UserSetting1" value="abc" /> </appSettings> </configuration>
Add a
Component
element toProduct.wxs
to include the file in the installation:<ComponentGroup Id="ProductComponents" ...