C# XML Descending order with elements and attributes -
i need find 5 maximum students xml file
my xml file looks this
<professor id="andy"> <floor nb="1"> <data> <room>101</room> <nbstudent>33724</nbstudent> </data> </floor> </professor > <professor id="mick"> <floor nb="2"> <data> <room>102</room> <nbstudent>33740</nbstudent> </data> </floor> </professor >
for code is
var xdoc = xdocument.load("student.xml"); var student= (from r in xdoc.descendants("data") orderby int.parse(r.xpathselectelement("nbstudent").value) descending select new { name = r.xpathselectelement("//professor").attribute("id").value + " ", room= r.xpathselectelement("room").value + " ", nb= r.xpathselectelement("nbstudent").value + " ", }) .take(5); foreach (var r in student) { console.writeline(r.name + r.room+ r.nb); }
it find 5 value want name return same (even if data after doesn't belong attribute.
i don't know how correct attribute.
another possible option fix problem minimal changes original code :
name = r.xpathselectelement("ancestor::professor[1]").attribute("id").value + " ",
Comments
Post a Comment