0

I have this XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Clients xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <set>
        <name>mario1</name>
        <sn>GSI100-A-00100</sn>
        <status>OK</status>
        <version>V</version>
    </set>
    <set>
        <name>mario2</name>
        <sn>GSI100-A-00101</sn>
        <status>OFF</status>
        <version>v49</version>
    </set>
    <set>
        <name>mario3</name>
        <sn>GSI100-A-00101</sn>
        <status>OK</status>
        <version>v49</version>
    </set>
</Clients>

and am trying to locate a node with <name> equal to the value of a variable (in this case $find) and trying to return the innertext of the <status> element of the same node. This is what I do, but all I get is null.

$xmledit = New-Object System.Xml.XmlDocument

$xmledit = "C:\Users\bigadmin\Desktop\Projects\AutoUpdate\test.xml"

[xml]$Xmlnew = Get-Content ($xmledit) -Encoding UTF8

$find = "mario1"

foreach ($search in $Xmlnew.clients.set) {
    $nodeexists = $search.SelectSingleNode("name") |
                  Where {$_.nodeexists -eq "$find"}

    return $nodeexists.clients.set.sn.InnerText
}
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • 1
    To just get the `Status` replace the whole foreach with `$xmlnew.clients.set | Where-Object Name -eq $find | Select-Object Status` or short without a header `($xmlnew.clients.set | Where-Object Name -eq $find).Status` –  Jun 04 '19 at 14:02
  • 3
    `SelectSingleNode("//set[name[text()='$find']/status")`? But since your XML data is namespaced you also need a [namespace manager](https://stackoverflow.com/a/18881581/1630171). – Ansgar Wiechers Jun 04 '19 at 14:15
  • @AnsgarWiechers ty got the logic. – Marios Anagnostopoulos Jun 05 '19 at 08:22

0 Answers0