How to return an XML file from a function with a node?

前端 未结 1 1636
南旧
南旧 2021-01-27 13:01

I have a simple XML



    10
    john
    
        pc24         


        
1条回答
  •  梦如初夏
    2021-01-27 13:52

    You can use a slightly smarter XPath to select your nodes, and then the InnerXml property to get the output string you're after:

    PS> $xml = [xml] @"
    
      
        10
        john
        
          pc24
        
      
      
        12
        peter
        
          pc25
        
      
    
    "@
    
    PS> $xml.SelectNodes("bds/bd[servers/name = 'pc25']").InnerXml
    12peterpc25
    

    Note - bds/bd[servers/name = 'pc25'] means "find all the bds/bd nodes that have a child servers/name node with a value pc25".

    You can then retro-fit this back into your function using your variable values in the XPath string as appropriate...

    0 讨论(0)
提交回复
热议问题