[英]Powershell XML Attribute
我有几个XML文件,需要从节点值读取和设置变量数据。
示例XML:
<Objs Version="1.1.0.1"
xmlns="http://schemas.microsoft.com/powershell/2004/04">
<Obj RefId="0">
<TN RefId="0">
<T>System.__ComObject#{86fd1ebe-92e2-40f3-9c03-e5f0ca55f8ab}</T>
<T>System.__ComObject</T>
<T>System.MarshalByRefObject</T>
<T>System.Object</T>
</TN>
<ToString>System.__ComObject</ToString>
<Props>
<S N="Name">copy1234</S>
</Props>
我正在编写的脚本必须读取文件,并将值“ copy1234”获取到变量中。 我很烂,所以它只返回NULL ...
Get-ChildItem $ImportFolderPath -Filter *.xml | Foreach-Object {
$currentFile = $_.FullName
Write-Host "Processing file: $currentfile"
[xml]$ccXML = Get-content $currentFile
$ccName = Select-Xml -Path $currentfile -Xpath '//objs/obj/props/S[@N="Name"]'
Write-Host "Name of node: $ccName"
您缺少XPath区分大小写的事实。
所以表达式应该是
$ccName = Select-Xml -Path $currentfile -Xpath '//Objs/Obj/Props/S[@N="Name"]'
如果Objs
是您的根元素,您甚至可以通过省略//
(文档中的所有位置)并使用/
(相对于根元素)来简化/优化此元素:
$ccName = Select-Xml -Path $currentfile -Xpath '/Objs/Obj/Props/S[@N="Name"]'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.