public void AddStudent(Student student) { var id = int.Parse(this.StudentsXmlDocument.Root.Attribute("last-id").Value) + 1; this.StudentsXmlDocument.Root.Attribute("last-id").SetValue(id); var idAttribute = new XAttribute("id", id); var firstnameAttribute = new XAttribute("firstname", student.FirstName); var lastnameAttribute = new XAttribute("lastname", student.LastName); var markElements = (from mark in student.Marks select new XElement("mark", new XAttribute("subject", mark.Subject), new XAttribute("score", mark.Score))); var marksElement = new XElement("marks", markElements); var studentElement = new XElement("student", idAttribute, firstnameAttribute, lastnameAttribute, marksElement); this.StudentsXmlDocument.Root.Add(studentElement); this.SaveDocument(); }
private Student ParseStudent(XElement studentElement) { int id = int.Parse(studentElement.Attribute("id").Value); string firstname = studentElement.Attribute("firstname").Value; string lastname = studentElement.Attribute("lastname").Value; IEnumerable<Mark> marks = (from markElement in studentElement.Element("marks").Elements("mark") select this.ParseMark(markElement)); var student = new Student() { Id = id, FirstName = firstname, LastName = lastname, Marks = marks }; return student; }