I have a simple XML
10
john
pc24
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...