I have a main class called theGarage
, which contains instances of our customer, supplier, and jobs classes.
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.
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();
}
}
}
This article shows you how to use the XmlTextReader class to read XML from a URL. The streamed information can come from kinds of sources, such as a byte stream from a server, a file, or a TextReader class.
startDocument() and endDocument() – Method called at the start and end of an XML document. startElement() and endElement() – Method called at the start and end of a document element. characters() – Method called with the text contents in between the start and end tags of an XML document element.
C# Program to Read and Parse an XML File Using XmlReader Class. The XmlReader class in C# provides an efficient way to access XML data. XmlReader. Read() method reads the first node of the XML file and then reads the whole file using a while loop.
There is much simpler way of serializing objects, use XmlSerializer
instead. See documentation here.
Code snippet to serialize your garage to file could look like:
var garage = new theGarage();
// TODO init your garage..
XmlSerializer xs = new XmlSerializer(typeof(theGarage));
TextWriter tw = new StreamWriter(@"c:\temp\garage.xml");
xs.Serialize(tw, garage);
And code to load garage from file:
using(var sr = new StreamReader(@"c:\temp\garage.xml"))
{
garage = (theGarage)xs.Deserialize(sr);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With