【发布时间】:2019-09-27 02:33:24
【问题描述】:
我正在尝试使用 powershell 解析 xml 文件。我想打印每个节点及其子节点。我是 xml 解析新手。
<?xml version="1.0" encoding="UTF-8"?>
<Inventory>
<Roles>
<Role Name="VirtualMachinePowerUser" Label="Virtual machine power user (sample)" Summary="Provides virtual machine interaction and configuration permissions">
<Privilege Name="Datastore.Browse" />
<Privilege Name="Global.CancelTask" />
<Privilege Name="ScheduledTask.Create" />
</Role>
<Role Name="VirtualMachineUser" Label="Virtual machine user (sample)" Summary="Provides virtual machine interaction permissions">
<Privilege Name="Global.CancelTask" />
<Privilege Name="ScheduledTask.Create" />
</Role>
下面是我的代码
[xml]$inputFile = Get-Content "C:\RolesnPer.xml"
$nodelist = $inputFile.Inventory.Roles.Role |Select-Object -Property Name
foreach ($Role in $nodelist)
{
$Role
$XMLprinterPath = $Role.selectSingleNode("Privilege").get_innerXml()
}
需要的输出:
Name Privilege
VirtualMachinePowerUser Datastore.Browse
Global.CancelTask
ScheduledTask.Create
VirtualMachineUser Global.CancelTask
ScheduledTask.Create
但是我得到了这个输出:
Name
----
VirtualMachinePowerUser
Method invocation failed because [Selected.System.Xml.XmlElement] does not contain a method named 'selectSingleNode'.
At C:\Mandy\Code\transformation.ps1:25 char:1
+ $XMLprinterPath = $Role.selectSingleNode("Privilege").get_innerXml()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (selectSingleNode:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
VirtualMachineUser
Method invocation failed because [Selected.System.Xml.XmlElement] does not contain a method named 'selectSingleNode'.
At C:\Mandy\Code\transformation.ps1:25 char:1
+ $XMLprinterPath = $Role.selectSingleNode("Privilege").get_innerXml()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (selectSingleNode:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
【问题讨论】:
标签: xml powershell parsing