Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use attributes when accessing XML nodes using dot-notation?

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?

like image 969
user310291 Avatar asked Oct 16 '25 16:10

user310291


1 Answers

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' }
like image 93
Ansgar Wiechers Avatar answered Oct 18 '25 09:10

Ansgar Wiechers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!