在多节点XML中调用元素的内部文本

时间:2019-06-04 13:41:31

标签: xml powershell

我有这个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>

,并且正在尝试找到一个<name>等于变量值(在这种情况下为$find)的节点,并试图返回该元素的<status>元素的内部文本节点。这就是我所做的,但是我得到的只是空。

$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
}

0 个答案:

没有答案