【问题标题】:Update xml specific value using PowerShell使用 PowerShell 更新 xml 特定值
【发布时间】:2013-11-28 08:41:23
【问题描述】:

假设我有这个 xml 文件

<section name="AAA">
    <Item1>FALSE</Item1>
    <Item2>FALSE</Item2>
    <Item3>FALSE</Item3>
</section>
<section name="BBB">
    <Item1>FALSE</Item1>
    <Item2>FALSE</Item2>
    <Item3>FALSE</Item3>
</section>

如何使用 PowerShell 更新特定值(使用 XPath 是正确的方法?)

意思是更新section name="BBB"下的Item2

【问题讨论】:

标签: xml powershell xpath


【解决方案1】:

假设您的 XML 有一个名为 root 的根节点,并且文档加载为 $doc,以下将在控制台上打印修改后的文档。

$doc.root.SelectSingleNode("section[@name='BBB']/Item2")."#text" = "True"
$doc.Save([console]::out)
# Output
<?xml version="1.0" encoding="ibm850"?>
<root>
  <section name="AAA">
    <Item1>FALSE</Item1>
    <Item2>FALSE</Item2>
    <Item3>FALSE</Item3>
  </section>
  <section name="BBB">
    <Item1>FALSE</Item1>
    <Item2>True</Item2>
    <Item3>FALSE</Item3>
  </section>
</root>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-08
    • 2020-05-29
    • 1970-01-01
    • 2020-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多