【问题标题】:WiX - How to add XML element only if it doesn't exist yetWiX - 如何仅在 XML 元素尚不存在时添加它
【发布时间】:2014-09-05 11:44:03
【问题描述】:

我想在安装期间向 XML 添加一个元素,但我想避免我的元素被升级安装复制。我怎样才能使我的XmlFile 组件有条件?

【问题讨论】:

  • 到目前为止你有没有尝试过?
  • 我想过使用<?if?><?endif?>模式,但是不知道如何判断文档是否包含该元素,以及如何将这种数据转化为条件的布尔表达式。。 .
  • @Elist - 在构建时(预处理器)进行评估,不能在运行时使用。

标签: xml wix duplicates conditional-statements


【解决方案1】:

您可以使用 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>

祝你好运!

【讨论】:

  • 根据文档:wixtoolset.org/documentation/manual/v3/xsd/util/xmlconfig.html,XmlConfig 需要一个必须创建或删除的操作。为什么你的某些 XmlConfig 没有指定 Action?
  • #Markmnl:这篇文章来自 2014 年。我认为那是因为我使用了旧的 wix 版本(WiX.Toolset.3.8.1128.0)
  • 等等,Action 不是必需属性,所以我认为“create”是默认值
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多