如何使用wix将多个元素添加到XML配置文件中? [英] How can multiple elements be added to an XML config file with wix?

查看:79
本文介绍了如何使用wix将多个元素添加到XML配置文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Wix编辑XML文件.我正在使用Wix 3.7附带的WixUtilExtension. xml文件是在Visual Studio 2010中为C#应用程序创建的设置文件.在此文件中,我正在使用一个元素,该元素用于在数组中存储多个字符串值.这是未更改的设置文件的内容:

I am trying to edit an XML file with Wix. I am using the WixUtilExtension bundled with Wix 3.7. The xml file is a settings file created in Visual Studio 2010 for a C# application. In this file, I am using an element which is used to store multiple string values in an array. This is the content of the unaltered settings file:

<configuration>
    <applicationSettings>
        <AppName.Properties.Settings>
            <setting name="StringArray" serializeAs="Xml">
                <value>
                    <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                        xmlns:xsd="http://www.w3.org/2001/XMLSchema">
                    </ArrayOfString>
                </value>
            </setting>
        </AppName.Properties.Settings>
    </applicationSettings>
</configuration>

我想在此文件的<ArrayOfString>元素中添加<string>元素.一种方法是使用wix/UtilExtension名称空间中的<XmlConfig>元素.我已将此元素添加到保存配置文件的组件中,如下所示:

I want to add <string> elements to the <ArrayOfString> element in this file. One way to do this is by using an <XmlConfig> element from the wix/UtilExtension namespace. I have added this element to the component which holds the config file like this:

<Component Id="ProductComponent" Guid="$(var.ConfigGuid)">
    <File Source="SettingsFile.exe.config" KeyPath="yes" Id="FILE_config" />
    <util:XmlConfig
      Name="string"
      Value="My value"
      File="[INSTALLFOLDER]SettingsFile.exe.config"
      Id="String1"
      On="install"
      Action="create"
      Node="element"
      ElementPath="/configuration/applicationSettings/AppName.Properties.Settings/setting[\[]@name='StringArray'[\]]/value/ArrayOfString"
      Sequence="100"
      />
</Component>

这将导致在<ArrayOfString>元素中添加一个<string>元素.要将另一个<string>元素添加到设置文件中,必须将另一个XmlConfig元素添加到安装项目的<Component>元素中,该元素具有不同的Id属性和较高的Sequence属性值,如下所示:

This results in the addition of one <string> element to the <ArrayOfString> element. To add another <string> element to the settings file, another XmlConfig element has to be added to the <Component> element of the setup project with a different Id attribute and a higher value for the Sequence attribute like this:

<util:XmlConfig
    Name="string"
    Value="My second value"
    File="[INSTALLFOLDER]SettingsFile.exe.config"
    Id="String2"
    On="install"
    Action="create"
    Node="element"
    ElementPath="/configuration/applicationSettings/AppName.Properties.Settings/setting[\[]@name='StringArray'[\]]/value/ArrayOfString"
    Sequence="101"
/>

安装msi后,设置文件中的<ArrayOfString>元素如下所示:

After installation of the msi, the <ArrayOfString> element in the settings file looks like this:

<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                            xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<string>My value</string><string>My second value</string></ArrayOfString>

我发现可以将<XmlConfig>属性的Value属性设置为这样的属性值:

I have found out that it is possible to set the Value attribute of an <XmlConfig> attribute to the value of a property like this:

<Property Id="STRING1VALUE" Value="My value" />
<util:XmlConfig Value="[STRING1VALUE]" ... />

这很好.我希望用户能够在安装过程中动态添加多个值,以便可以将可变数量的<string>元素添加到设置文件中. 我的第一种方法是使用这样的<?foreach?>语句:

This is good. I would like the user to be able to add multiple values in the installation process dynamically so that a variable amount of <string> elements can be added to the settings file. My first approach was to use a <?foreach?> statement like this:

<?define values="My value;My second value"?>
<?foreach value in $(var.values)?>
    <util:XmlConfig
        Name="string"
        Value="$(var.value)"
        File="[INSTALLFOLDER]SettingsFile.exe.config"
        Id="String$(var.value)"
        On="install"
        Action="create"
        Node="element"
        ElementPath="/configuration/applicationSettings/AppName.Properties.Settings/setting[\[]@name='StringArray'[\]]/value/ArrayOfString"
        Sequence="101"
    />
<?endforeach?>

此方法存在一些问题:

  1. foreach语句使用一个预处理器变量,该变量不能设置为属性的值.
  2. 序列"属性的值保持不变.

我希望用户将字符串元素的值存储在属性中,该属性用分号将值分开,然后在如下的foreach语句中解析它们:

I would like the user to store the values for the string elements in a Property which separates the values by semicolons and then parse them in a foreach statement like this:

<Property Id="VALUES" Value="My value;My second value" />
<?foreach value in [VALUES]?>
    <util:XmlConfig
        Name="string"
        Value="$(var.value)"
        File="[INSTALLFOLDER]SettingsFile.exe.config"
        Id="String$(var.value)"
        On="install"
        Action="create"
        Node="element"
        ElementPath="/configuration/applicationSettings/AppName.Properties.Settings/setting[\[]@name='StringArray'[\]]/value/ArrayOfString"
        Sequence="101"
    />
<?endforeach?>

这将引发以下错误:

The util:XmlConfig/@Id attribute's value, 'String[VALUES]', is not a legal identifier. 
Identifiers may contain ASCII characters A-Z, a-z, digits, underscores (_), or periods (.). 
Every identifier must begin with either a letter or an underscore.

是否可以使用XmlFile或XmlConfig元素创建可变数量的元素?解决此问题的唯一方法是CustomAction吗?

Is there any way I can create a variable amount of elements with the XmlFile or the XmlConfig element? Is the only solution to this problem a CustomAction?

推荐答案

基于Rob的回答,这是我使用Wix将多个元素添加到XML配置文件中的新方法.我不想编写C ++代码,这就是为什么我使用

Based on Rob's answer, here is my new approach to adding multiple elements to an XML config file with Wix. I did not want to write C++ code, that is why I used DTF in my CustomAction.

我将描述如何使用定界符将包含多个元素的字符串转换为多个XML元素.

I am going to describe how to turn a string containing multiple elements using a delimiter into multiple XML elements.

首先,安装文件中必须有一个包含定界字符串的属性.

First there needs to be a property in the setup file containing the delimited string.

<Property Id="STRINGARRAY" Value="string1;string2;string3" />

用户当然可以在对话框中填充此属性.

This property could be populated by the user in a dialog, of course.

接下来,必须编写一个CustomAction.若要使用DTF,必须将对Microsoft.Deployment.WindowsInstaller.dll的引用添加到C#CustomAction项目中.该项目中的using指令应包含名称空间Microsoft.Deployment.WindowsInstaller.我的CustomAction如下所示:

Next, a CustomAction has to be written. To make use of the DTF, a reference to the Microsoft.Deployment.WindowsInstaller.dll has to be added to the C# CustomAction project. The namespace Microsoft.Deployment.WindowsInstaller should be included with a using directive in that project. My CustomAction looks like this:

[CustomAction]
public static ActionResult Insert(Session session)
{
    string strings = session["STRINGARRAY"];
    string[] stringArray = strings.Split(';');
    Database db = session.Database;
    View view = db.OpenView("select * from `XmlConfig`");
    string xpath = "/configuration/applicationSettings/AppName.Properties.Settings/setting[\\[]@name='StringArray'[\\]]/value/ArrayOfString";
    for (int i = 0; i < stringArray.Length; i++)
    {
        string id = String.Format("String{0}", i);
        int sequence = 100 + i;
        string value = stringArray[i].Trim();
        Record rec = new Record(
            id,
            "[INSTALLFOLDER]SettingsFile.exe.config",
            xpath,
            null,
            "string",
            value,
            273,
            "ProductComponent",
            sequence);
        view.InsertTemporary(rec);
    }
    db.Close();
    return ActionResult.Success;
}

在这里,首先将属性StringArray读入局部变量,该局部变量将转换为字符串数组.下一行建立了与安装程序使用的当前数据库的连接.将在表XmlConfig上创建一个句柄,该表是XML元素添加到的表.要将正确的值插入该表中,最好创建一个包含该表的安装程序文件,然后在诸如orca或InstEd之类的编辑器中查看该表.

Here, at first the Property StringArray is read into a local variable which is converted to a string array. The following line establishes a connection to the current database used by the installer. A handle on the table XmlConfig is created, which is the table where the XML elements are added to. To insert the right values into that table, it is best to create an installer file which contains such a table and then take a look at that table in an editor like orca or InstEd.

在xpath中,必须使用双反斜杠来转义反斜杠. id变量使用一个简单的字符串保存临时记录的名称,并且数字可以完美地工作.每个元素的顺序必须增加.我找不到关于flags列的值的任何文档,但是我发现对于创建的元素,其值设置为273,对于删除的元素,其值设置为289.

In the xpath, backslashes have to be escaped by using double backslashes. The id variable holds the name of the temporary record, using a simple string and a number works flawlessly. The sequence has to be incremented for each element. I could not find any documentation on the values of the flags column, but I have found out that its value is set to 273 for elements that are created and 289 for elements that get deleted.

一旦记录中填充了正确的值,就可以使用视图对象的InsertTemporary方法将其添加到XmlConfig表中.这是对定界字符串中找到的每个元素完成的.

Once the record is filled with the correct values, it gets added to the XmlConfig table by using the InsertTemporary method of the view object. This is done for each element found in the delimited string.

我遇到的一个问题是,如果XmlConfig表不存在,则这个CustomAction失败.为了解决这个问题,我将以下代码添加到安装文件中,该文件将一个元素添加到XML文件中并立即删除该元素.我猜可能有一个更干净的解决方案,但这对我来说是最简单的解决方案.

A problem I have come across is that this CustomAction fails, if the XmlConfig table does not exist. To counter this problem I have added the following code to the setup file, which adds an element to the XML file and immediately deletes that element. I guess there could be a cleaner solution, but this was the easiest one for me.

<util:XmlConfig
    Name="string"
    Value="Dummy"
    File="[INSTALLFOLDER]SettingsFile.exe.config"
    Id="DummyEntry"
    On="install"
    Action="create"
    Node="element"
    ElementPath="/configuration/applicationSettings/AppName.Properties.Settings/setting[\[]@name='StringArray'[\]]/value/ArrayOfString"
    Sequence="1" />
<util:XmlConfig
    On="install"
    Action="delete"
    Id="DeleteDummyEntry"
    Node="element"
    File="[INSTALLFOLDER]SettingsFile.exe.config"
    VerifyPath="/configuration/applicationSettings/AppName.Properties.Settings/setting[\[]@name='StringArray'[\]]/value/ArrayOfString/string"
    ElementPath="/configuration/applicationSettings/AppName.Properties.Settings/setting[\[]@name='StringArray'[\]]/value/ArrayOfString"
    Sequence="2" />

最后,必须将CustomAction添加到安装项目中.通过在设置项目中添加对CustomAction项目的引用,可以如下指定二进制文件的位置:

Finally, the CustomAction has to be added to the setup project. By adding a reference to the CustomAction project in the setup project, the location of the binary can be specified like this:

<Binary Id="XmlCustomActionDLL" SourceFile="$(var.XmlCustomAction.TargetDir)XmlCustomAction.CA.dll" />

必须立即执行CustomAction,否则它将无法访问会话变量:

The CustomAction has to be executed immediately, otherwise it won't be able to access the session variable:

<CustomAction Id="CA_XmlCustomAction" BinaryKey="XmlCustomActionDLL" DllEntry="Insert" Execute="immediate" Return="check" />
<InstallExecuteSequence>
  <Custom Action="CA_XmlCustomAction" Before="RemoveRegistryValues" />
</InstallExecuteSequence>

要确定CustomAction在安装顺序中的正确位置,我依靠

To determine the right position for the CustomAction in the installation sequence, I relied on this article by Bob Arnson.

这篇关于如何使用wix将多个元素添加到XML配置文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆