我有几个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"发布于 2018-03-13 06:15:35
您忽略了一个事实,即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"]'https://stackoverflow.com/questions/49245039
复制相似问题