说我有以下xml
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
</book>
<book id="bk103">
<author>Corets, Eva</author>
</book>
</catalog>
我有一个xmlNode代表我通过执行操作从文件中读取的整个文件
$myXml=New-Object XML
$myXml.Load("path to the books file")
然后,我该如何选择id值为“bk102”的book元素,以便随后将该Xmlnode传递给另一个需要XMlNode的函数?
即我的新节点将是:
$Node = <book id="bk102">
<author>Ralls, Kim</author>
</book>
谢谢大家整个早晨都在挣扎。
最佳答案
您可以使用Where-Object
过滤书籍,也可以尝试XPath。这是两个示例:
点导航
$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
$bookid = 'bk102'
$myxml = [xml](Get-Content '.\New Text Document.txt')
$node = $myxml.SelectSingleNode("catalog/book[@id='$bookid']")
$node
id author
-- ------
bk102 Ralls, Kim