0

We use wix to modify xml during install. We need to add a list of xml elements to a file. Example list:

<Item>
    <Address>some@address.com</Address>
</Item>
<Item>
    <Address>someother@address.com</Address>
</Item>

Now we want to add this to an xml file under a specific node.

How can we do this in wix?

We already use XmlFile to modify other parts of the xml, can we use this or do we have to use a custom action?

Julius
  • 946
  • 1
  • 10
  • 26

1 Answers1

0

You can use the XMLFIle element itself to create the nodes that you want. You should use the following attribute within the XMLFILE node.

Action = "createElement"

Creates a new element under the element specified in ElementPath. The Name attribute is required in this case and specifies the name of the new element. The Value attribute is not necessary when createElement is specified as the action. If the Value attribute is set, it will cause the new element's text value to be set

For example: We want the installer to add new nodes under the settings node:

<settings>   
      <add key="a_key" value="a_value">key_item
        <inside>inside_item</inside>   
      </add> 
</settings>

You should have the following within your wix script to achieve this:

<util:XmlFile Id='XmlSettings1' File='[INSTALLDIR]settings.xml'
    Action='createElement' Name='add' ElementPath='//settings' Sequence='1' />
  <util:XmlFile Id='XmlSettings2' File='[INSTALLDIR]settings.xml'
    Action='setValue' Name='key' Value='a_key' ElementPath='//settings/add' Sequence='2' />
  <util:XmlFile Id='XmlSettings3' File='[INSTALLDIR]settings.xml'
    Action='setValue' Name='value' Value='a_value' ElementPath='//settings/add' Sequence='3' />
  <util:XmlFile Id='XmlSettings4' File='[INSTALLDIR]settings.xml'
    Action='setValue' Value='key_item' ElementPath='//settings/add' Sequence='4' />
  <util:XmlFile Id='XmlSettings5' File='[INSTALLDIR]settings.xml'
    Action='createElement' Name='inside' ElementPath='//settings/add' Sequence='5' />
  <util:XmlFile Id='XmlSettings6' File='[INSTALLDIR]settings.xml'
    Action='setValue' Value='inside_item' ElementPath='//settings/add/inside' Sequence='6' />

XMLFILE EXample XMLFILE Element

Isaiah4110
  • 9,855
  • 1
  • 40
  • 56
  • The problem is that we do not know how many element to add. This is entered by the user in the installer GUI. I think your suggestion only works when you know the exact number of elements. But it is an idea to setup a maximum number of elements... – Julius Jan 30 '14 at 20:59
  • I did not know that since that was not explicitly called out in the question. WIX has foreach loop (check the iteration statements section) in this link http://wixtoolset.org/documentation/manual/v3/overview/preprocessor.html BUt I think in your case it would be easier to create a custom action to do the job. – Isaiah4110 Jan 30 '14 at 21:15