Question:
Say I have the following xml
1 2 3 4 5 6 7 8 9 10 11 12 13 |
I have an xmlNode representing the whole file which I read in from a file by doing
1 2 3 |
$myXml=New-Object XML $myXml.Load("path to the books file") |
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 :
1 2 3 4 |
$Node = |
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
1 2 3 4 5 6 7 8 9 10 |
$bookid = 'bk102' $myxml = [xml](Get-Content '.\New Text Document.txt') $node = $myxml.catalog.book | Where-Object { $_.id -eq $bookid } $node id author -- ------ bk102 Ralls, Kim |
XPath
1 2 3 4 5 6 7 8 9 10 |
$bookid = 'bk102' $myxml = [xml](Get-Content '.\New Text Document.txt') $node = $myxml.SelectSingleNode("catalog/book[@id='$bookid']") $node id author -- ------ bk102 Ralls, Kim |