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

前端 未结 1 1620
南旧
南旧 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] @"
    <bds>
      <bd>
        <id>10</id>
        <user>john</user>
        <servers>
          <name>pc24</name>
        </servers>
      </bd>
      <bd>
        <id>12</id>
        <user>peter</user>
        <servers>
          <name>pc25</name>
        </servers>
      </bd>
    </bds>
    "@
    
    PS> $xml.SelectNodes("bds/bd[servers/name = 'pc25']").InnerXml
    <id>12</id><user>peter</user><servers><name>pc25</name></servers>
    

    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)
提交回复
热议问题