xml - 使用 powershell 检索 xml 值
问题描述
我想使用 powershell 转换检索 xml 标签中的信息。但是这个错误消息是我所面临的:
Select-Xml:无法将值“System.Xml.XmlDocument”转换为类型“System.Xml.XmlDocument”。错误:“指定的节点不能作为该节点的有效子节点插入,因为指定的节点类型错误。”
[xml]$xml=
@"
<BESAPI xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="BESAPI.xsd">
<Computer Resource="https://dsw11.lab.g:52311/api/computer/549"/>
<Computer Resource="https://dsw11.lab.g:52311/api/computer/550"/>
<Computer Resource="https://dsw11.lab.g:52311/api/computer/552"/>
<Computer Resource="https://dsw11.lab.g:52311/api/computer/551"/>
</BESAPI>
"@
#I have tried these alternatives for result, but both of them are not working
#$result = Select-Xml -Content $xml -XPath "//Computer Resource" | foreach {$_.node.InnerXML}
$result = Select-Xml -Content $xml -XPath "//Computer Resource" | foreach {[pscustomobject]@($_.node.InnerXML)}
我想从计算机资源中检索 ID。再次为此,
foreach($str in $result)
{
$res = $str.split('/')[-1]
# next steps
}
我搜索了很多,但没有任何工作。
提前感谢您的帮助。
解决方案
Select-Xml -xml $xml -XPath "//Computer" |
foreach { $_.node.Resource.split('/')[-1] }
549
550
552
551
推荐阅读
- python - 如何从广告创意 Facebook 营销 API 获取广告 ID
- javascript - 获取客户端javascript代码中可用的node.js服务器端对象
- r - 使用特定列的值从其他行中减去,但列索引在 R 中的文件之间移动
- spring - 使用反应式 DB 客户端扩展 Spring WebFlux 应用程序时出现问题:Maven 依赖项冲突?
- git - 如何 grep Git 提交消息或关键字的差异并输出更改(聚合)的文件名?
- javascript - CakePHP scriptBlock 没有在 HTML 页面中添加任何 JavaScript
- node.js - 为什么我不能直接使用 req.body 作为参数?
- javascript - React Native,为什么 react-native run-android 不起作用?
- hadoop - 使用 cygwin 在窗口 10 上配置 Hive:路径错误
- python - 在 pylint 和 mypy 中处理 Python 类重新定义警告