使用 XPath 在 WIX 中删除带有 XmlConfig 扩展名的 XML 元素 [英] Deleting XML element with XmlConfig extension in WIX using XPath

查看:19
本文介绍了使用 XPath 在 WIX 中删除带有 XmlConfig 扩展名的 XML 元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 XmlFile 扩展来删除 XML 文件中的条目,准确地说是这个:<元素名称="Somename" attribute2="whatever" provider-name="whatever2" type="DotNet">

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

这是我的 XML 文件

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

这是我的 Wix 代码

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

当我运行安装程序时,它会重写 file.xml,但它看起来是一样的.它不会删除 -> <元素名称="Somename" attribute2="whatever" provider-name="whatever2" type="DotNet">

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

我做错了什么?我在构建时没有遇到任何错误,可能是我的 xpath 错误?

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

推荐答案

  1. 要删除 XML 元素,您需要使用 XMLConfig 元素

XMLFIile 元素用于更新/删除元素内的属性:

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

deleteValue -从 ElementPath 中指定的元素中删除一个值.如果指定了 Name,则删除具有该名称的属性.如果未指定 Name,则删除 ElementPath 中指定的元素的文本值.如果 deleteValue 是指定的操作,则忽略 Value 属性.

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.

由于您没有提到名称"属性,它正在尝试删除文本值.您在 ELEMENT 中没有文本值,因此即使在编辑后您的文件也保持不变.

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.

更新:使用 WIX 脚本更新答案

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

在上面的脚本中,您需要使用您的 xml 文件的 ID 更新#filename".

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

希望这会有所帮助!

这篇关于使用 XPath 在 WIX 中删除带有 XmlConfig 扩展名的 XML 元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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