using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Linq; using HelperFunctions; using System.IO; namespace GradingAssistant { public class GradeConverter { public static void Convert(IEnumerable> students, string outputDest) { var element = new XElement("students", from studentRecord in students select new XElement("student", new XElement("studentId", studentRecord.Key.StudentID), new XElement("grade", studentRecord.Value)) ); element.Save(outputDest); } private static bool isNumber(string input) { try { int.Parse(input); return true; } catch (FormatException) { return false; } } public static Dictionary LoadGrades(string gradeXML, List allStudents) { XDocument scoreXML = XDocument.Load(gradeXML); var students = from student in scoreXML.Descendants("student") let studentID = student.Element("studentId").Value let localStudent = allStudents.Find(s => s.StudentID == studentID) where localStudent != null let scoreString = student.Element("grade").Value where isNumber(scoreString) let score = int.Parse(scoreString) select new KeyValuePair(localStudent, score); return students.Distinct(s => s.Key.StudentID).ToDictionary(kvp => kvp.Key, kvp => kvp.Value); } } }