gpt4 book ai didi

xml - 使用PowerShell替换嵌套XML中的值

转载 作者:行者123 更新时间:2023-12-03 00:27:16 25 4
gpt4 key购买 nike

如何使用Powershell替换嵌套xml中的值?

如何使用PowerShell将“Racers”,“Pacers”,“Losers”的值从“false”替换为“True”?

<Tester>
<TesterSection>
<section name="TestingApp" type="Amazon,Google" />
</TesterSection>
<application>
<add key="Minimum" value="1" />
<add key="Maximum" value="true" />
<add key="Runners" value="N/A" />
<add key="Racers" value="false" />
<add key="Pacers" value="false" />
<add key="Losers" value="false" />
</application>
</Tester> ```

最佳答案

你可以试试这个

# Simulate the XML in a string
$text = @"
<Tester>
<TesterSection>
<section name="TestingApp" type="Amazon,Google" />
</TesterSection>
<application>
<add key="Minimum" value="1" />
<add key="Maximum" value="true" />
<add key="Runners" value="N/A" />
<add key="Racers" value="false" />
<add key="Pacers" value="false" />
<add key="Losers" value="false" />
</application>
</Tester>
"@

$xml = [xml]$text
# You can load the file with
# $xml = [xml] "c:\temp\thefile.xml"

# List of keys to change
$tochange = "Racers","Pacers","Losers"

foreach ($add in $xml.Tester.application.add)
{
# value before
$add.value
# modification
if ($tochange -contains $add.key)
{
$add.value= "true"
}
# name after
$add.value
}
# Or in a sigle line
#$xml.Tester.application.add | % {if ($_.value -eq "false"){$_.Value ="true"}}
$xml.Save("c:\temp\FileAfter.xml")

关于xml - 使用PowerShell替换嵌套XML中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61553226/

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