gpt4 book ai didi

xml - 通过索引更新重复的子节点

转载 作者:行者123 更新时间:2023-12-03 01:14:45 28 4
gpt4 key购买 nike

我正在尝试使用Powershell通过索引更新单个XML子节点属性。在下面的示例中,我只想更新第二个Import Filename =“File_2.txt”属性

<!--test.xml -->
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<node1 test="hello"/>
<node2>
<Import Filename="File_1.txt" />
<Import Filename="File_2.txt" />
<Import Filename="File_3.txt" />
</node2>
</configuration>

([xml] ( gc "test.xml" )).configuration.node1.test #displays hello
([xml] ( gc "test.xml" )).configuration.node2.Import.Filename[1] #displays File_2.txt

好吧,看来我有正确的节点引用,所以现在我更新子节点
$xml = ([xml] ( gc "test.xml" ))
$xml.configuration.node1.test = "something new"
$xml.Save( "test.xml" )
$xml.configuration.node1.test #Displays something new

$xml = ([xml] ( gc "test.xml" ))
$xml.configuration.node2.Import.Filename[1] = "ImaNewFile.txt"
$xml.Save( "test.xml" )
$xml.configuration.node2.Import.Filename[1] #Displays File_2.txt

configuration.node1.test已更新为 ,是新的
configuration.node2.Import.Filename [1]仍显示 File_2.txt
为什么第二个configuration.node2.Import.Filename没有更新?

最佳答案

更改

$xml.configuration.node2.Import.Filename[1] = "ImaNewFile.txt"


$xml.configuration.node2.Import[1].Filename = "ImaNewFile.txt"

我相信$ xml.configuration.node2.Import.Filename只是一个字符串数组,它与xml节点没有连接,但是我相信$ xml.configuration.node2.Import [1]是对实际xml节点的引用。

关于xml - 通过索引更新重复的子节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32507220/

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