3
votes

I'm trying to use XmlFile extension to delete entries in a XML file, this one to be precise: < Element name="Somename" attribute2="whatever" provider-name="whatever2" type="DotNet">

Here is my XML File

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Elements>
    <Element name="Somename" attribute2="whatever" provider-name="whatever2" type="DotNet"></Element>
    <Element name="Somename2" attribute2="whatever" provider-name="whatever2" type="DotNet"></Element>
</Elements>

Here is my Wix code

<Feature Id='Config' Level='1'>
        <Component KeyPath="yes" Id="UpdateConfig" Guid="{xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}" Directory="MyCommonAppDataFolders">
                    <util:XmlConfig Action="delete" Id="RemoveAnElement" Node="element" File="path\to\my\file.xml" VerifyPath='/Elements/Element[\[]@name=\"Somename"[\]]' ElementPath="/Elements/Element" Sequence="1" />
        </Component>
        <Condition Level="1">FILEEXISTS</Condition>
    </Feature>      

When I run the setup, it rewrites the file.xml, but it looks the same. It doesn't delete -> < Element name="Somename" attribute2="whatever" provider-name="whatever2" type="DotNet">

What am I doing wrong? I don't get any errors while building, maybe my xpath is wrong?

1
I think you have some extra brackets in your xpath. Can you try this: //Element\[@name='Somename'\]"deanosaur
@deanosaur nope, that's how wix escapes brachetsGerald Hughes

1 Answers

6
votes
  1. To delete XML Elements you need to make use of the XMLConfig Element

  2. The XMLFIile Element is used to update/delete attributes within an element:

    deleteValue - Deletes a value from the element specified in the ElementPath. If Name is specified, the attribute with that name is deleted. If Name is not specified, the text value of the element specified in the ElementPath is deleted. The Value attribute is ignored if deleteValue is the action specified.

Since you have not mentioned the "Name" attribute it is trying to delete the text value. You dont have a text value in the ELEMENT and hence your file remains the same even after the edit.

UPDATE: Updated answer with the WIX script

<util:XmlConfig Id='SetXMlfiletest'
              File='[#filename]'
              Action='delete'
              Node='element'
              ElementPath="/Elements"
              On='install'
              PreserveModifiedDate='yes'
              VerifyPath="/Elements/Element[\[]@name='Somename'[\]]"
              Sequence="1"    />

In the above script you need to update the "#filename" with the ID of your xml file.

Hope this helps!