XML 在Powershell中使用childnode属性修改父节点

XML Parent node modification using childnode attribute in Powershell

我正在尝试使用 Powershell 中的子节点属性修改父节点属性。做这个的最好方式是什么?非常感谢任何指点。下面是代码。文件的名称是 pricefile.xml

 <model type="model1" name="default" price="12.12" date="some_value">
  <PriceData>
    <item name="watch" price="24.28" date="2013-12-01"/>
    <item name="toy" price="22.34" date="2013-12-02"/>
    <item name="bread" price="24.12" date="2013-12-03"/>
  </PriceData>
 </model>

我想使用项目名称过滤上述 xml 文件。例如,如果我需要 item watch 的详细信息,我应该能够在 powershell 中解析上面的 xml 并获得以下内容。

 <model type="model1" name="watch" price="24.28" date="2013-12-01">
  <PriceData>
    <item name="watch" />
  </PriceData>
 </model>

注意子节点的属性是如何被删除的,模型属性是如何用观察属性重置的。你能告诉我最好的方法吗?

如果我使用以下任何命令,我都不会得到任何输出。

    [xml]$item = get-content pricefile.xml

    $item.SelectNodes("//item/@*").parentnode

    $item.SelectNodes("//item/@*") | where {$_.parentnode}

xml 文档 parsing/traversing 的类似问题中提供了一个非常优雅的解决方案。 它也适用于遍历这个问题中的树。 以下是对另一个问题的link: