在powershell中使用xpath选择xml中的属性 [英] Selecting attributes in xml using xpath in powershell

查看:51
本文介绍了在powershell中使用xpath选择xml中的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 powershell 和 XPath 来选择以下 xml 示例中显示的名称属性.

I am trying to use powershell and XPath to select the name attribute shown in the below xml example.

     $xml_peoples= $file.SelectNodes("//people") 
     foreach ($person in $xml_peoples){
            echo $person.attributes
            #echo $person.attributes.name
     }

上面是我运行以尝试获取名称的代码,但它似乎不起作用.有什么建议吗?

Above is the code im running to try and get the name, but it doesn't seem to work. Any suggestions?

<peoples>
    <person name='James'>
        <device>
            <id>james1</id>
            <ip>192.192.192.192</ip>
        </device>
    </person>
</peoples>

提前致谢!

推荐答案

我不确定 $hub 是什么,而且你从中间开始你的代码所以不清楚你是否正确设置$file 到 XmlDocument 对象,但我认为这是您想要的:

I'm not sure what $hub is, and you started your code from the middle so it's not clear if you properly set $file to an XmlDocument object, but I think this is what you want:

[System.Xml.XmlDocument]$file = new-object System.Xml.XmlDocument
$file.load(<path to XML file>)
$xml_peoples= $file.SelectNodes("/peoples/person")
foreach ($person in $xml_peoples) {
  echo $person.name
}

这篇关于在powershell中使用xpath选择xml中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆