C# – How to Save Complex Data to File or Disk

cdata structuresnet

I am working on a side project for a small business owner (my father) and part of the requirements for the application include saving customer and job related data in a way that it can easily be backed up and/or restored on demand.

I am familiar with how to save data to a flat file, and I am familiar with how to save data to a database. What I feel like I am missing is what are "typical" strategies for saving data such as I described above to a file?

For example I noticed Quickbooks has their own file format that encapsulates an entire "business" in one file that can easily be backed up and/or used for restoration. (Briefly) How is that accomplished?

I have come across integrated databases such as sqlExpress and am left wondering if this is how end user applications typically manage data or if it is something else entirely.

In short, how can I encapsulate a large amount of complex data into a single file either of a simple data type (i.e. .DAT/.TXT) or possibly a custom file format?

EDIT: As an added bit of information I would like to avoid having to install a stand-alone RDBMS in order to save data to a database.

Best Answer

Building on Franck's comment, you can use a DataSet to save to XML.

Writing DataSet Contents as XML Data

System.IO.StreamWriter xmlSW = new System.IO.StreamWriter("Customers.xml");

// This example assumes you have a custom dataset called ds holding customer data
ds.WriteXml(xmlSW, XmlWriteMode.WriteSchema);

xmlSW.Close();

This will produce an XML file called Customers.xml in your current directory that is a serialized version of your DataSet.

You will also need to know how to load that data back into your objects... Loading a DataSet from XML

// Load the dataset from a file called input.xml
DataSet ds = new DataSet();
ds.ReadXml("Customers.xml", XmlReadMode.ReadSchema);