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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
aikimarkCommented:
Yep.  Those were the two.
0
aikimarkCommented:
Solution.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.