
Use DataSet to Store Application Settings
by Roman Rehak
January 2003 Issue
Technology Toolbox: C#, XML, ADO.NET
Writing software applications often involves storing and retrieving application settings, such as saved options, user preferences, and the application's state when the user closes it. You can save these settings to the Registry, binary files, text files, or XML files. ADO.NET provides you with yet another technique for storing and retrieving application settings: You can use the DataSet object to hold them and persist them into a file easily.
You normally use datasets to hold results of database queries, but you can also create them on the fly and populate them programmatically. The DataSet object has characteristics that make it suitable for persisting application settings: It can hold hierarchical data in the Tables collection, and you can use the WriteXml() method to save the entire dataset into an XML file with a simple call:
ds.WriteXml(strFileName,
XmlWriteMode.WriteSchema);
Saving the schema with the data guarantees that the structure will remain the same when you reload the DataSet object. Use the ReadXml() method to load the persisted dataset from a file:
ds.ReadXml(strFileName);
You can use either the relational object model (DataTable, DataRow, DataColumn) or the XML object model to manipulate the data in the dataset, depending on which one you're more comfortable using. You can pass the DataSet object to an XmlDataDocument object's constructor to use the XML.NET API:
XmlDataDocument xmlDD = new
XmlDataDocument(AnyDataSet);
Back to top
|