xml - 使用 MSBuild.ExtensionPack.Xml.XmlFile UpdateElement 更新 XML 文件

标签 xml xpath msbuild

我有一个 MSBUILD 项目文件,我正在尝试 checkout 一些 xml 文件并更新版本元素,但似乎无法让它为我的生活工作。这似乎应该是一个简单的解决方案,但我无法让它工作。我有一个简单的 xml 文件:

<?xml version="1.0" standalone="yes"?>
<DataSet>
  <Profile>
    <Product>ProductA</Product>
    <Version>1.2.3.4</Version>
  </Profile>
  ~
</DataSet>

我可以使用 xmlPeek 获取版本:
<XmlPeek XmlInputPath="c:\myProfile.xml"
  Query="DataSet/Profile/Version[../Product = &quot;ProductA&quot;]/text()">
  <Output TaskParameter="Result" ItemName="ProductVersion" />
</XmlPeek>

我没有运气使用 XmlPoke 更新版本,所以我从 XmlFile 的类页面尝试了 XmlFile 的 UpdateElement:

http://www.msbuildextensionpack.com/help/4.0.5.0/html/4009fe8c-73c1-154f-ee8c-e9fda7f5fd96.htm
  <MSBuild.ExtensionPack.Xml.XmlFile
    TaskAction="UpdateElement"
    File="c:\machine.config"
    XPath="/configuration/configSections/section[@name='system.data']"
    InnerText="NewValue"/>

我尝试了以下以及许多其他 XPath 版本,但都没有。
  <MSBuild.ExtensionPack.Xml.XmlFile
    TaskAction="UpdateElement"
    File="c:\myProfile.xml"
    XPath="/DataSet/Profile/Version[@Product='ProductA']"
    InnerText="5.6.7.8"
  />

我错过了什么?或者有人知道 xmlpoke 的正确 XPath

谢谢

最佳答案

在 XPath 中,@用于引用属性,如 @attribute_name .所以你尝试的 XPath 的一部分 Version[@Product='ProductA'] , 将寻找 Version具有 Product 的元素属性值等于“ProductA”,类似于以下内容(在您的 XML 中不存在):

<Version Product="ProductA"/>

获取 Version 的正确 XPath元素 Product名称如下:
/DataSet/Profile[Product='ProductA']/Version

这个特殊的部分Profile[Product='ProductA'] , 过滤器 Profile具有子元素的元素 Product等于“产品A”。

关于xml - 使用 MSBuild.ExtensionPack.Xml.XmlFile UpdateElement 更新 XML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37259357/

相关文章:

java - Android 从类路径加载文件导致崩溃

sql-server - 将 varchar() 中的重音字符转换为 XML 导致 "illegal XML character"

msbuild - 自定义 MSBuild 任务锁定程序集

php - DOMXPath - 获取节点

java - struts2中不允许显示字段错误的拦截器

sql-server - 检查 XML 元素是否在 SQL 中标记为 `xsi:nil`

php - SimpleXmlElement 和 XPath,获取空数组()

python - 如何使用 Scrapy 从网站获取所有纯文本?

visual-studio-2013 - 用于构建服务器的 Typescript 1.4 SDK?

msbuild - AfterTargets 失败不会导致 msbuild run 失败