How to select an XmlElement based on an attribute value?

后端 未结 1 616
[愿得一人]
[愿得一人] 2021-01-24 22:37

Say I have the following xml



   
      Gambardella, Matthew           


        
相关标签:
1条回答
  • 2021-01-24 23:35

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

    Dot-navigation

    $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
    
    0 讨论(0)
提交回复
热议问题