Note that there are some explanatory texts on larger screens.

plurals
  1. POEasily write a whole class instance to XML File and read back in
    primarykey
    data
    text
    <p>I have a main class called <code>theGarage</code>, which contains instances of our customer, supplier, and jobs classes.</p> <p>I want to save the program data to an XML file, I have used the code below (just a snippet, I have matching code for the other classes). I am wondering if there is an easier way for me to do this, like write the whole theGarage class to an XML file and read it in without having to write all this code like I have below. </p> <pre><code> 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(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload