private void btnSave_Click(object sender, EventArgs e) { try { StreamWriter sw = new StreamWriter("config.xml"); XmlSerializer writer = new XmlSerializer(typeof(CarCollection)); CarCollection overview = new CarCollection(); List <Car> container = new List <Car>(); foreach (Label lt in lbstate.container) { Car item = new Car(); item.Make = lt.Text; item.Model = lt.Location.Y; container.Add(item); //MessageBox.Show(lt.Location.Y.ToString()); } overview.cars = container; writer.Serialize(sw, overview); MessageBox.Show("Save Okay"); } catch { MessageBox.Show("error"); } }
private void button1_Click(object sender, EventArgs e) { this.Controls.Clear(); InitializeComponent(); //string path = "cars.xml"; XmlSerializer sr = new XmlSerializer(typeof(CarCollection)); StreamReader reader = new StreamReader("config.xml"); CarCollection readcar = new CarCollection(); //List<Car> container = new List<Car>(); readcar = (CarCollection)sr.Deserialize(reader); foreach (Car item in readcar.cars) { MessageBox.Show(item.Model.ToString()); } }