【发布时间】:2019-01-16 19:48:45
【问题描述】:
我有一个 XML 文件 file1.xml,其中包含如下数据。
<Tag1>
<Tag2>
<file Name="file1.rdl" Path="folder34" />
<File Name="file2.rdl" Path="folder37" />
<File Name="file3.rdl" Path="folder34" />
<File Name="File4.rdl" Path="folder76" />
<File Name="file5.rdl" Path="folder37" />
</Tag2>
</Tag1>
我需要一个 PowerShell 脚本,它将根据 Name 属性值返回 Path 属性的值。
例子:
如果我传递值“File4.rdl”,它应该将值返回为“Folder76”。
我编写了读取 XML 数据的命令,并将数据存储在数组 $Test 中。
[xml]$XmlDocument = Get-Content "D:\Roshan\Testing\Test1.xml"
$Test = $XmlDocument.Tag1.Tag2.file | Select-Object Name, Path
Write-Host $Test
我编写了下面的 PowerShell 脚本来执行相同的操作。
$AttribureReportName = "File4.rdl"
foreach ($item in $Test) {
if ($item.Name -eq $AttribureReportName) {
$FinalFolder = $item.Path
Write-Output $FinalFolder
}
}
由于我有太多 XML 标记,因此执行该操作需要很长时间。有没有更好的方法来做同样的事情?
【问题讨论】:
标签: xml powershell