Pregunta

Tengo un documento XML con la siguiente estructura:

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

¿Cuál es la mejor manera de conseguir un elemento <Fruit> por su código (o cualquier otro campo) en PowerShell 1 código? (No XPath, como se admite en PowerShell 2 solamente)

Gracias!

¿Fue útil?

Solución

Se puede utilizar XPath en V1 como este, si lo prefiere:

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

Code                                                        Name
----                                                        ----
2                                                           Orange

Otros consejos

Se puede acceder a los nodos como objetos de 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 }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top