With the updated XML I revised my answer -
I think this is what you're looking for. You would just have to update the path to your xml.
$XMLPath = "ENTER_PATH"
function Get-StudentInfo {
param (
$xPATH,
$title)
Select-Xml -Path $XMLPath -XPath "/school/students/$xPATH" | ForEach-Object { $_.Node.$title }
}
$students = [PSCustomObject]@{
Names = Get-StudentInfo -xPath "student" -title "name"
Subjects = Get-StudentInfo -xPath "student" -title "subject"
Years = Get-StudentInfo -xPath "student" -title "year"
SIDs = Get-StudentInfo -xPath "student/details" -title "SID"
Codes = Get-StudentInfo -xPath "student/details" -title "code"
Groups = Get-StudentInfo -xPath "student/details" -title "group"
}
$students.Names
From there you would have an object $students that has properties with an array for each property.
Names : {Bob, John}
Subjects : {Math, Science}
Years : {5, 5}
SIDs : {38343555, 38343555}
Codes : {1123, 1123}
Groups : {, }
To get a list of all the students names you would use $students.names or for subject $students.subjects
A version that loads the XML file only once, instead of once per call to Get-StudentInfo:
$students = Select-Xml -Path "test.xml" -XPath '/school/students/student' | Select-Object @(
@{name='Name'; expr={ $_ | Select-Xml '@name' }}
@{name='Subject'; expr={ $_ | Select-Xml '@subject' }}
@{name='Year'; expr={ $_ | Select-Xml '@year' }}
@{name='SID'; expr={ $_ | Select-Xml 'details/@SID' }}
@{name='Code'; expr={ $_ | Select-Xml 'details/@code' }}
@{name='Group'; expr={ $_ | Select-Xml 'details/@group' }}
)
$students
result
Name Subject Year SID Code Group
---- ------- ---- --- ---- -----
Bob Math 5 38571273 1122
John Science 5 38343555 1123
SID="38343555"is not a valid tag name.