Reading XML file using Powershell

This post deals with reading XML file and then traversing through the nodes of XML and fetching node values using powershell commands.

consider the below xml fragment with file path location

<STUDENTINFO>
<student ID="1" name="Sathya">
</student>
<student ID="2" name="Deepak">
</student>
</STUDENTINFO>


#loading xml from mentioned path location into xml variable
[xml]$Xmlvar = Get-Content D:\myxml.xml 


#fetching values of xml nodes

$firststudent = $Xmlvar.STUDENTINFO.student[0]
$firststudent

$secondstudent = $Xmlvar.STUDENTINFO.student[1]
$secondstudent 
 
 
foreach( $student in $Xmlvar.STUDENTINFO.student)

{

Write-host $student.name
Write-host $student.Id

}  


$Xmlvar.SelectSingleNode("/STUDENTINFO/student")

$Xmlvar.SelectNodes("/STUDENTINFO/student")







No comments: