web 2.0

.NET Trick - Persisting Data Without Using a Database

There have been many occasions where I have developed an application where I want to persist data between sessions without using SQL Server. Therefore, I came up with the idea of using the DataSet's built-in ReadXML() and LoadXML() methods to persist data to disk as an XML file.

In an application that I am currently building I added the ability to launch external tools from a File Menu. I chose not to store this data in the central database because the application can move from machine to machine and the applications the end user makes shortcuts to, may differ on each workstation. I could have used the registry but to be honest I hate the registry. Anyway, In my application, I created a static class to handle the data manipulation. The basic plumbing looks something like this:

   1:  public static List<ExternalTool> LoadToolsFromDisk()
   2:  {
   3:     List<ExternalTool> tools = new List<ExternalTool>();
   4:   
   5:     if (File.Exists(Filename) == false) return tools;
   6:     DataSet ds = new DataSet(DATASET_NAME);
   7:     ds.ReadXml(Filename);
   8:     DataTable table = ds.Tables[TABLE_NAME];
   9:   
  10:     foreach (DataRow row in table.Rows) {
  11:        ExternalTool tool = new ExternalTool();              
  12:        tool.Title = GetFieldValue( row, COL_TITLE );
  13:        tool.Command = GetFieldValue( row, COL_COMMAND );    
  14:        tool.Arguments = GetFieldValue( row, COL_ARGUMENTS );
  15:        tools.Add(tool);
  16:     }
  17:     return tools;
  18:  }

The code starts by creating a new DataSet. If there is an xml file on disk matching the pre-determined filename then the ReadXML() method is called. Once ReadXML() finishes you can manipulate the data as usual. Just iterate over the rows in the DataTable to load it into the generic list. On the flipside, saving the data is also very simple:

   1:  public static void SaveToolsToDisk( List<ExternalTool> tools )
   2:  {
   3:     DataSet ds = new DataSet(DATASET_NAME);
   4:     DataTable table = new DataTable(TABLE_NAME);
   5:     table.Columns.Add(COL_TITLE, typeof(String));
   6:     table.Columns.Add(COL_COMMAND, typeof(String));
   7:     table.Columns.Add(COL_ARGUMENTS, typeof(String));         
   8:   
   9:     foreach (ExternalTool tool in tools) {
  10:        DataRow row = table.NewRow();
  11:        row[COL_TITLE] = tool.Title ?? String.Empty;
  12:        row[COL_COMMAND] = tool.Command ?? String.Empty;
  13:        row[COL_ARGUMENTS] = tool.Arguments ?? String.Empty;
  14:        table.Rows.Add(row);
  15:     }
  16:     ds.Tables.Add(table);
  17:     ds.AcceptChanges();       
  18:     ds.WriteXml(Filename);
  19:  }

The code creates a new DataSet from a generic list, shoves the contents of the list into a DataTable and then writes the data to disk using the WriteXML() method. The whole process is very simple and easy to maintain. Happy Coding!

Tags: ,

blog comments powered by Disqus