I am trying to parse an xml file using powershell. I want to print each node and its subnodes. I am new to xml parsing.
<?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>
My code below
[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()
}
Required output:
Name Privilege
VirtualMachinePowerUser Datastore.Browse
Global.CancelTask
ScheduledTask.Create
VirtualMachineUser Global.CancelTask
ScheduledTask.Create
But I am getting this output:
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