본문 바로가기
C#(CSharp)/Etc

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

by swconsulting 2018. 6. 25.



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);
}


SOURCE : https://stackoverflow.com/questions/13266496/easily-write-a-whole-class-instance-to-xml-file-and-read-back-in