I have a main class called theGarage , which contains instances of the classes of our customers, suppliers and jobs.
I want to save the program data to an XML file, I used the code below (just a fragment, I have the corresponding code for other classes). I am wondering if I have an easier way to do this, for example to write the entire TheGarage class to an XML file and read it without having to write all this code, as shown below.
public void saveToFile() { using (XmlWriter writer = XmlWriter.Create("theGarage.xml")) { writer.WriteStartDocument(); /// writer.WriteStartElement("theGarage"); writer.WriteStartElement("Customers"); foreach (Customer Customer in Program.theGarage.Customers) { writer.WriteStartElement("Customer"); writer.WriteElementString("FirstName", Customer.FirstName); writer.WriteElementString("LastName", Customer.LastName); writer.WriteElementString("Address1", Customer.Address1); writer.WriteElementString("Address2", Customer.Address2); writer.WriteElementString("Town", Customer.Town); writer.WriteElementString("County", Customer.County); writer.WriteElementString("PostCode", Customer.Postcode); writer.WriteElementString("TelephoneHome", Customer.TelephoneHome); writer.WriteElementString("TelephoneMob", Customer.TelephoneMob); //begin vehicle list writer.WriteStartElement("Vehicles"); foreach (Vehicle Vehicle in Customer.Cars) { writer.WriteStartElement("Vehicle"); writer.WriteElementString("Make", Vehicle.Make); writer.WriteElementString("Model", Vehicle.Model); writer.WriteElementString("Colour", Vehicle.Colour); writer.WriteElementString("EngineSize", Vehicle.EngineSize); writer.WriteElementString("Registration", Vehicle.Registration); writer.WriteElementString("Year", Vehicle.YearOfFirstReg); writer.WriteEndElement(); } writer.WriteEndElement(); writer.WriteEndElement(); } } }
developer__c
source share