Assume I have this XML file:
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book id="book1">
<chapters>
<chapter1>Begin</chapter1>
<chapter2>mid</chapter2>
<chapter3>end</chapter3>
</chapters>
<pages>352</pages>
</book>
<book id="book2">
<chapters>
<chapter1>woop</chapter1>
<chapter2>doop</chapter2>
<chapter3>goop</chapter3>
</chapters>
<pages>761</pages>
</book>
</books>
Now I want to read this with PowerShell, e.g.:
$file = "C:\path\books.xml"
$xml = [xml](Get-Content $file)
$books = $xml.books.book.id
foreach ($book in $books) {
...
}
In the loop I want to get the chapters of each book, so I tried this:
$xml.books.book | ? {$_.id -eq $book} | select chapters.chapter1
but that didn't work. So I tried this instead:
(xml.books.book | where $_.id -eq $book).chapters.chapter1
Both won't work for some reason. How can I get these values?