Xml Serialization C# - Error with List<T>

bravozulu

New Member
I've searched for this on Google but found no help.I have a \[code\]List<T>\[/code\] that I serialize, and when I resume my application and deserialize I got redoubled the element of the collection (if I have for example \[code\]"a,b,c"\[/code\], I got \[code\]"a,b,c,a,b,c,"\[/code\]).It's strange cause if I debug with a messagebox after the serialization I got the right number of elements but in the XML file I see objects two times.Code that might help:\[code\]XmlSerializer writer = new XmlSerializer( typeof(WpfApplication6.Logic.Configuration), new Type[] { typeof(Exam) });StreamWriter file = new StreamWriter( Directory.GetCurrentDirectory() + "/Serialization.xml"); writer.Serialize(file, configuration);file.Close();\[/code\]\[code\]Configuration\[/code\] contains \[code\]Profile\[/code\] that contains \[code\]Plan\[/code\] (the class where is created my \[code\]List<T>\[/code\])Any idea? It's a known problem? \[code\]public class Configuration : IConfiguration{ private Profile profile; public Configuration() { this.profile = new Profile("", "", "", ""); } public Profile Profile { get { return this.profile; } set { this.profile = value; } }}\[/code\]class Plan (Profile Class it's like the others, it just contains Plan)\[code\]public class Plan : IPlan{ public List<Exam> exams; public Plan() { this.exams = new List<Exam>(); } public List<Exam> Exams { get { return this.exams; } set { this.exams = value; } } // other stuff}\[/code\]main class\[code\]public partial class MainWindow : Window{ public Configuration configuration; private Profile profile; private Plan plan; private List<ExamControl> examControls; private int i; private int x; private int y; private int margin; private int cont; public MainWindow() { this.cont = 0; this.i = 0; this.margin = 0; this.x=240; this.y=127; examControls = new List<ExamControl>(); InitializeComponent(); if (!File.Exists(@Environment.CurrentDirectory + "/Serialization.xml")) { firstRun(); } else { try // deserialization { XmlSerializer serializer = new XmlSerializer(typeof(Configuration), new Type[] { typeof(Exam) }); System.IO.StreamReader file = new System.IO.StreamReader(Directory.GetCurrentDirectory() + "/Serialization.xml"); this.configuration = (Configuration)serializer.Deserialize(file); this.profile = configuration.Profile; this.plan = profile.Plan; file.Close(); } catch (Exception s) { MessageBox.Show("errore nel recupero impostazioni salvate"); } // update fields textBox1.Text = profile.Name; textBox2.Text = profile.Surname; textBox3.Text = profile.Uni; textBox4.Text = profile.Year; foreach (Exam a in plan.Exams) { DrawExamFirstTime(a); } } } public void firstRun() // first run of the application { this.configuration = new Configuration(); this.profile = configuration.Profile; this.plan = profile.Plan; } public void DrawExam(Exam a) { cont++; if (cont < 14) { margin = (cont - 1) * 35; ExamControl excontrol = new ExamControl(a, this, plan); examControls.Add(excontrol); i++; excontrol.Margin = new Thickness(x, y + margin, 0, 0); grid1.Children.Add(excontrol); } else { if (cont >= 14 && cont < 29) { margin = (examControls.Count - i) * 35; ExamControl excontrol = new ExamControl(a, this, plan); examControls.Add(excontrol); excontrol.Margin = new Thickness(650, y + margin, 0, 0); grid1.Children.Add(excontrol); } } } public void DrawExamFirstTime(Exam a) { cont++; if (cont < 14) { margin = (cont - 1) * 35; ExamControl excontrol = new ExamControl(a, this, plan); examControls.Add(excontrol); i++; excontrol.Margin = new Thickness(x, y + margin, 0, 0); grid1.Children.Add(excontrol); excontrol.examBox.Text = a.Name; excontrol.cfuBox.Text = "" + a.CFU; excontrol.lodeBox.IsChecked = a.Lode; excontrol.comboBox1.Text = "" + a.Voto; } else { if (cont >= 14 && cont < 29) { margin = (examControls.Count - i) * 35; ExamControl excontrol = new ExamControl(a, this, plan); examControls.Add(excontrol); excontrol.Margin = new Thickness(650, y + margin, 0, 0); grid1.Children.Add(excontrol); excontrol.examBox.Text = a.Name; excontrol.cfuBox.Text = "" + a.CFU; excontrol.lodeBox.IsChecked = a.Lode; excontrol.comboBox1.Text = "" + a.Voto; } } } private void addButton_Click(object sender, RoutedEventArgs e) { if (examControls.Count<29) { Exam a = new Exam("", 0, 0, false, false); plan.addExam(a); DrawExam(a); } else { MessageBox.Show("Errore! Hai inserito il numero massimo di esami"); } } private void Window_Closing(object sender, CancelEventArgs e) { // serialization XmlSerializer writer = new XmlSerializer(typeof(WpfApplication6.Logic.Configuration), new Type[] { typeof(Exam) }); System.IO.StreamWriter file = new System.IO.StreamWriter(Directory.GetCurrentDirectory() + "/Serialization.xml"); writer.Serialize(file, configuration); MessageBox.Show("ci sono examcontrols " + examControls.Count + "\n ci sono planexams " + plan.countExams()); file.Close(); } public void Refresh() { examLabel.Content = plan.countExams(); cfuLabel.Content = plan.countCFU(); mediaLabel.Content = (float)plan.calculateaverage(); laureaLabel.Content = plan.calculatevotoLaurea(); if (plan.calculatevotoLaurea() == 110) { l3Label.Content = 110; l4Label.Content = 110; } else { l3Label.Content = plan.calculatevotoLaurea() * 1.0325; l4Label.Content = plan.calculatevotoLaurea() * 1.016; } } // other stuff}\[/code\]
 
Back
Top