3

我有一个具有这种结构的 XML 文档:

<Fruits>
    <Fruit>
        <Code>1</Code>
        <Name>Apple</Name>
    </Fruit>
</Fruits>

<Fruit>在 PowerShell 1 代码中通过其代码(或任何其他字段)获取元素的最佳方法是什么?(不是 XPath,因为它仅在 PowerShell 2 中受支持)

谢谢!

4

2 回答 2

5

您可以从 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 }
于 2010-05-26T08:52:02.040 回答
3

如果您愿意,可以像这样在 V1 中使用 XPath:

$xml = [xml](get-content $xmlFile)
$xml.SelectSingleNode("//Fruit[2]")

Code                                                        Name
----                                                        ----
2                                                           Orange
于 2010-05-26T09:08:30.813 回答