How to do it...

  1. Start off by creating a Student class.
        public class Student
{
public string StudentName;
public double SubjectMark;
}
  1. Next, create a class for a subject called FundamentalProgramming. Several attributes have been applied to the fields of this class:
    • XmlRoot
    • XmlElement
    • XmlIgnore
    • XmlAttribute
    • XmlArray

We can see that the XmlRoot attribute specifies that ElementName be called FundamentalsOfProgramming. This attribute thus defines the root of your generated XML. The XmlElement specifies an element called LecturerFullName instead of Lecturer.  XmlIgnore attribute will cause  XmlSerializer to ignore this field during serialization, while  XmlAttribute will create an attribute on the root element of the generated XML. Lastly, we are serializing the List<Student> collection with the XmlArray attribute:

        [XmlRoot(ElementName = "FundamentalsOfProgramming", 
Namespace = "http://serialization")]
public class FundamentalProgramming
{
[XmlElement(ElementName = "LecturerFullName",
DataType = "string")]
public string Lecturer;

[XmlIgnore]
public double ClassAverage;

[XmlAttribute]
public string RoomNumber;

[XmlArray(ElementName = "StudentsInClass",
Namespace = "http://serialization")]
public List<Student> Students;
}
  1. In the calling code, set up the Student objects and add them to the List<Student> object students:
        string serializationPath = @"C:tempclassInfo.xml";
Student studentA = new Student()
{
StudentName = "John Smith"
, SubjectMark = 86.4
};
Student studentB = new Student()
{
StudentName = "Jane Smith"
, SubjectMark = 67.3
};
List<Student> students = new List<Student>();
students.Add(studentA);
students.Add(studentB);
  1. Now we create our FundementalProgramming class and populate the fields. The reason why ClassAverage is ignored is because we will always calculate this field value:
        FundamentalProgramming subject = new FundamentalProgramming();
subject.Lecturer = "Prof. Johan van Niekerk";
subject.RoomNumber = "Lecture Auditorium A121";
subject.Students = students;
subject.ClassAverage = (students.Sum(mark => mark.SubjectMark) /
students.Count());
  1. Add the following code to serialize the subject object, taking note to pass the type of object to  XmlSerializer as typeof(FundamentalProgramming):
        using (FileStream stream = new FileStream(serializationPath, 
FileMode.Create))
{
XmlSerializer xmlSer = new XmlSerializer(typeof(
FundamentalProgramming));
xmlSer.Serialize(stream, subject);
}
  1. Lastly, add the code to deserialize the XML back into the FundamentalProgramming object:
        using (FileStream stream = new FileStream(serializationPath, 
FileMode.Open))
{
XmlSerializer xmlSer = new XmlSerializer(typeof(
FundamentalProgramming));
FundamentalProgramming fndProg = (FundamentalProgramming)
xmlSer.Deserialize(stream);
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.172.146