4

PowerShell has dot syntax for accessing some XML nodes:

root.object1.object2

Is it possible to do something like this?

root.object1.object2[@id="pdt1"]

except this syntax doesn't work.

Does this kind of syntax exist or is it mandatory to use SelectNodes() method?

1
  • 1
    You can use Where-Object: root.object1.object2|? id -eq pdt1. Commented Aug 16, 2015 at 8:45

1 Answer 1

6

No, you can't mix object and XPath syntax like that. Either use XPath:

$xml.SelectSingleNode('/root/object1/object2[@id="pdt1"]')

or use object syntax with a Where-Object filter (as suggested by @PetSerAl in the comments to your question):

$xml.root.object1.object2 | Where-Object { $_.id -eq 'pdt1' }
Sign up to request clarification or add additional context in comments.

1 Comment

I didn't know about Where-Object thanks. I wish they'll add the more natural syntax in the future.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.