PowerShell:检索特定的内部XML元素

rke*_*erm 2 xml powershell powershell-1.0

我有一个具有以下结构的XML文档:

<Fruits>
    <Fruit>
        <Code>1</Code>
        <Name>Apple</Name>
    </Fruit>
</Fruits>
Run Code Online (Sandbox Code Playgroud)

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

谢谢!

ste*_*tej 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 }
Run Code Online (Sandbox Code Playgroud)