SelectSingleNode search - powershell

Hey

<root>
 <employee id="1234" client="123">
<name>Joe</name>
</employee>
</root>

Open in new window


How do I search for employee id = 1234 (using SelectSingleNode - Powershell)

Mike
LVL 1
mikeydkAsked:
Who is Participating?
 
oBdACommented:
Indeed, thanks; copy and paste error, the SelectSingleNode slipped up one line.
And on closer inspection, the slash in the XPath should be a forward-slash ...
But the Here-String should remain:
$xml = [xml]@'
<root>
	<employee id="1234" client="123">
	<name>Joe</name>
	</employee>
</root>
'@ 
$xml.SelectSingleNode("root/employee[@id='1234']")

Open in new window

0
 
oBdACommented:
You need the xml path to the node, then the comparison of the attribute inside square brackets:
root\employee[@id='1234']

Open in new window

From your example:
$xml = [xml]@'
<root>
	<employee id="1234" client="123">
	<name>Joe</name>
	</employee>
</root>
$xml.SelectSingleNode("root\employee[@id='1234']")
'@

Open in new window

Keep in mind that XML nodes and attribute names and values are case sensitive!
0
 
aikimarkCommented:
@oBdA
I think you have a couple of problems with your example.
$xml = [xml]'
<root>
	<employee id="1234" client="123">
	<name>Joe</name>
	</employee>
</root>
'
$xml.SelectSingleNode("root/employee[@id='1234']")

Open in new window

0
 
aikimarkCommented:
Yep.  Those were the two.
0
 
aikimarkCommented:
Solution.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.