xml - 在powershell中使用xpath选择xml中的属性

标签 xml powershell xpath scripting

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

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

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

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

提前致谢!

最佳答案

这两行就足够了:

[xml]$xml = Get-Content 'C:\path\to\your.xml'
$xml.selectNodes('//person') | select Name

关于xml - 在powershell中使用xpath选择xml中的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17583373/

相关文章:

java - 如何使用 JAXB 编码没有 XML 根元素的 java 列表?

arrays - 使用数组索引中的变量输出 $Error

security - 使用 powershell Set-GPPermissions cmdlet 设置 GPO 安全过滤器

java - 如何有效管理各种转换任务?

html - 抓取页面源代码的可靠方法,即每行开头的电视?

xml - 如何将参数 "null"转换为 null

xml - 查找包含文本的xPath

powershell - 如何在 PowerShell 中的函数结束时返回用户菜单

ruby - Xpath Nokogiri嵌套

Android TextView 禁用颜色更改