gpt4 book ai didi

xml - 在 powershell 中访问 XMLAttribute 的 #text 属性

转载 作者:数据小太阳 更新时间:2023-10-29 01:54:49 26 4
gpt4 key购买 nike

我有一个格式如下的 xml 文档:

<root>
<obj>
<indexlist>
<index name="NUMD" value="val1" />
<index name="DATE" value="val2" />
</indexlist>
</obj>
</root>

现在我想更改名称设置为“DATE”的索引元素的值属性。我得到这样的属性:

$attr = $xml.selectnodes("//obj/indexlist/index[@name='DATE']/@value")

我可以通过键入以下内容来查看值:

$attr.'#text'

但我不能改变它:

$attr.'#text' = 'foo'
The property '#text' cannot be found on this object. Verify that the property exists and can be set.
At line:1 char:1
+ $n.'#text' = 'foo'
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException

如何更改 XMLAttribute 的值?


如果可能的话,我还想坚持使用直接返回属性的 XPath,因为该脚本的最终用户将使用 XPath 在配置文件中定义要更改的元素和属性。
在对属性使用 XPath 的同时,用户可以简单地提供要更改的属性和 future 值,只需两个参数:XPath 和值。

最佳答案

除了#text,您还可以通过Value属性访问XmlAttribute的值:

$attr = $xml.SelectSingleNode("//obj/indexlist/index[@name='DATE']/@value")

#print old value
$attr.Value

#update attribute value
$attr.Value = "new value"

#print new value
$attr.Value

请注意,$attr.Value 中的ValueXmlAttribute 的属性名。它不受 XML 中名为 value 的属性的影响。

关于xml - 在 powershell 中访问 XMLAttribute 的 #text 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29259590/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com