简体   繁体   中英

how do I get a specific value from XML with same names

I'm trying to get the value for one of the branches in the below screenshot - the value is <ParamValue> with the blue arrow.

The problem is that there are so many brances with the same name and I think the only way I want to filter them is on the name (don't want to do CommandID that that might chnage in the future) - so, PackageName="intel_teaming_script.ps1" .

How can I do this? I tried the below but it does not show anything back:

[xml]$xml = Get-content C:\temp\build.xml
    $node = $xml.SelectSingleNode("//Phases/Phase/Package[@PackageName='intel_teaming_script']")
    $node.PackageName

I also tried this and still not getting it:

$findxml = $xml.Phases.Phase.Package | Where-Object {$_.PackageName -eq 'intel_teaming_script.ps1'}
$findxml.PackageName

Even if I do the above I still need to dig down and get the value out - which is a IP address.

Screenshot of XML below:

在此处输入图片说明

UPDATE: Based on @har07 answer which works the below is what I get as an answer:

$node = $xml.SelectSingleNode("//Phases/Phase/Package[@PackageName='intel_teaming_script.ps1']//ParamValue")

ParamValue

#text                                                                                                                                                                                                                                                            
-----                                                                                                                                                                                                                                                            
1.1.1.1  

If I try converting it to a String and then using ExpandProperty I still get ParamValue listed..any idea how I can just get the IP address?

$node = $xml.SelectSingleNode("//Phases/Phase/Package[@PackageName='intel_teaming_script.ps1']//ParamValue")
$node.ToString()
$node | select -ExpandProperty "#text"

Result:

ParamValue
1.1.1.1

You can get element by position index in xpath, for example :

//Phases/Phase/Package[@PackageName='intel_teaming_script.ps1']//Param[2]/ParamValue

Above xpath return the 2nd <ParamValue> found. Note that xpath position index starts from 1 instead of 0.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM