Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WiX - How to add XML element only if it doesn't exist yet

I want to add an element to XML during installation, but I want to avoid my element to be duplicated by upgrade installations. How can I make my XmlFile Component conditional?

like image 730
Elist Avatar asked Dec 09 '25 20:12

Elist


1 Answers

You can use XmlConfig! Imagine, I need to create connectionstring (check if exist) like that:

<add name="MyConnection" connectionString="this-is-connection-string" providerName="System.Data.SqlClient" />

And this is my code:

<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi"
     xmlns:util="http://schemas.microsoft.com/wix/UtilExtension">

    <Fragment>
        <Property Id="CONNECTIONSTRING_NAME" Value="MyConnection" />

        <SetProperty Id="ThisIsDynamicValue" 
                     Value="/configuration/connectionStrings/add[\[]@name='[CONNECTIONSTRING_NAME]'[\]]"
                     After="InstallInitialize" Sequence="execute" />

        <DirectoryRef Id="INSTALLFOLDER">
            <Component Id="C_ConnectionString" Guid="{35E351D5-1154-4EA0-91AE-F898EEF8C551}">

                <!-- First: Make sure that exist a connectionString with name is "MyConnection" -->

                <!--Create "add" element-->
                <util:XmlConfig Id="CreateConnectionString" Action="create" On="install" Node="element"
                                File="web.config" VerifyPath="[ThisIsDynamicValue]"
                                Name="add" 
                                ElementPath="/configuration/connectionStrings"
                                Sequence="1" />

                <!--Create "name" attribute-->
                <util:XmlConfig Id="attrName"
                               File="web.config" ElementId="CreateConnectionString"
                               Name="name" Value="[CONNECTIONSTRING_NAME]" 
                               Sequence="2" />

                <!--Create "connectionString" attribute-->
                <util:XmlConfig Id="attrConnString"
                               File="web.config" ElementId="CreateConnectionString"
                               Name="connectionString" Value="this-is-connection-string" 
                               Sequence="3" />

                <!--Create "providerName" attribute-->
                <util:XmlConfig Id="attrProvider"
                               File="web.config" ElementId="CreateConnectionString"
                               Name="providerName" Value="System.Data.SqlClient"
                               Sequence="4" />

                <!--Second: Update connectionString-->
                <util:XmlFile   Id="SpecificScriptConnectionString" Action="setValue" Permanent="yes"
                                SelectionLanguage="XSLPattern" Sequence="1" Name="connectionString"
                                File="web.config"
                                ElementPath="[ThisIsDynamicValue]" Value="this-is-new-value-of-connectionstring-after-updated" />

                <CreateFolder />
            </Component>
        </DirectoryRef>
    </Fragment>
</Wix>

Good luck!

like image 167
minhhungit Avatar answered Dec 11 '25 12:12

minhhungit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!