我有以下XML,我正在尝试获取Name
属性为ComponentVersion
的值。
<Component .......>
<Settings>
<Setting Name="ComponentVersion" Value="12345" />
<Setting Name="Host" Value="false" />
</Settings>
<Jobs>
<Job Name="Platform" />
</Jobs>
</Component>
我读了这样的XML文件:
$fullPathXML = "$env:PackagesPath\Platform\Components\SystemPlatform.Comp.xml"
[xml]$xmlSystemPlatform = Get-Content $fullPathXML
然后尝试了以下内容:
$xmlSystemPlatform.selectNodes('//Settings/Setting') | select Name
Select-Xml '//Settings/Setting[contains(@Name, "ComponentVersion")]' $xmlSystemPlatform |%{$_.Node.Value}
$xmlSystemPlatform.Settings.Setting[0].Name
$xmlSystemPlatform.Settings.Setting[0].Value
答案 0 :(得分:2)
$xmlSystemPlatform.Component.Settings.Setting | ? Name -eq "ComponentVersion" | select Value
答案 1 :(得分:1)
使用这样的XPath expression应该有效:
$filename = "$env:PackagesPath\Platform\Components\SystemPlatform.Comp.xml"
[xml]$xml = Get-Content $filename
$xml.SelectNodes('//Settings/Setting[@Name = "ComponentVersion"]').Value
请注意,XPath表达式区分大小写。
答案 2 :(得分:0)
另一个单行:
([Xml] (Get-Content "$env:PackagesPath\Platform\Components\SystemPlatform.Comp.xml")).SelectNodes("//Settings/Setting[@Name='ComponentVersion']").Value
或者如果您喜欢变量:
$xmlSystemPlatform.SelectNodes("//Settings/Setting[@Name='ComponentVersion']").Value