【发布时间】:2020-12-30 16:37:16
【问题描述】:
我是 Powershell 的新手,我不想获取此 xml 的所有 id(无法更改 xml 因为这只是我的问题的一个例证)
<?xml version="1.0" encoding="ISO-8859-1"?>
<List>
<Person1>
<Id>E00023</Id>
<empName>Aadharsh</empName>
</Person1>
<Person2>
<Id>E00042</Id>
<empName>Raksha</empName>
</Person2>
</List>
使用此代码,我只能得到 Person1 的 ID:
$XMLfile = 'C:\test.xml'
[XML]$empDetails = Get-Content $XMLfile
foreach($module in $empDetails.List.Person1){
Write-Host "Id :" $module.Id
}
我尝试了以下代码,但我不工作:( 问题是 Person1 和 Person2 是不同的名称。 我必须更改什么才能获得所有 ID?
$XMLfile = 'C:\test.xml'
[XML]$empDetails = Get-Content $XMLfile
foreach($module in $empDetails.List.$module){
Write-Host "Id :" $module.Id
}
【问题讨论】:
标签: xml powershell parsing xml-parsing powershell-3.0