How to select an XmlElement based on an attribute value?

Question:

Say I have the following xml

I have an xmlNode representing the whole file which I read in from a file by doing

How do I then select the book element which has the id value of “bk102” so I can then pass that Xmlnode off to another function expecting an XMlNode?

i.e my new node would be :

Thanks all. Been struggling all morning.

Answer:

You could filter the books using Where-Object, or you could try XPath. Here’s two examples:

Dot-navigation


XPath

Source:

How to select an XmlElement based on an attribute value? by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply