Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easily write a whole class instance to XML File and read back in

Tags:

c#

file

xml

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();
            }
        }
    }
like image 790
developer__c Avatar asked Nov 07 '12 09:11

developer__c


People also ask

Which class in C# would you use to connect to an XML file and read it?

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.

How read and write XML?

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.

What is the best way to read XML in C #?

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.


Video Answer


1 Answers

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);
}
like image 100
Michal Klouda Avatar answered Oct 24 '22 05:10

Michal Klouda