WiX - 如何仅在尚不存在的情况下添加XML元素

时间:2014-07-15 10:11:52

标签: xml wix duplicates conditional-statements

我想在安装期间向XML添加一个元素,但我想避免升级安装复制我的元素。如何使XmlFile组件成为条件?

1 个答案:

答案 0 :(得分:5)

你可以使用XmlConfig! 想象一下,我需要创建连接字符串(检查是否存在):

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

这是我的代码:

<?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>
祝你好运!