簡體   English   中英

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

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

我想在安裝期間向XML添加一個元素,但我想避免升級安裝復制我的元素。 如何使我的XmlFile組件有條件?

你可以使用XmlConfig! 想象一下,我需要創建connectionstring(檢查是否存在),如下所示:

<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>

祝好運!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM