我有一个具有这种结构的 XML 文档:
<Fruits>
<Fruit>
<Code>1</Code>
<Name>Apple</Name>
</Fruit>
</Fruits>
<Fruit>
在 PowerShell 1 代码中通过其代码(或任何其他字段)获取元素的最佳方法是什么?(不是 XPath,因为它仅在 PowerShell 2 中受支持)
谢谢!
我有一个具有这种结构的 XML 文档:
<Fruits>
<Fruit>
<Code>1</Code>
<Name>Apple</Name>
</Fruit>
</Fruits>
<Fruit>
在 PowerShell 1 代码中通过其代码(或任何其他字段)获取元素的最佳方法是什么?(不是 XPath,因为它仅在 PowerShell 2 中受支持)
谢谢!
您可以从 Posh V1 访问像对象一样的节点
$xml = [xml]"<Fruits>
<Fruit>
<Code>1</Code>
<Name>Apple</Name>
</Fruit>
<Fruit>
<Code>2</Code>
<Name>Orange</Name>
</Fruit>
</Fruits>"
$orange = $xml.Fruits.Fruit | ? { [int]$_.Code -eq 2 }
如果您愿意,可以像这样在 V1 中使用 XPath:
$xml = [xml](get-content $xmlFile)
$xml.SelectSingleNode("//Fruit[2]")
Code Name
---- ----
2 Orange