Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Application State Data using XML

0.00/5 (No votes)
26 Sep 2006 1  
A utility to capture/restore state data to a container via XML.

Sample Image

Introduction

What follows is a utility that iterates through the controls in a container and saves or restores values for the controls in an XML file. The enclosed namespace captures application data, also known as sticky data or state data. The concept is the same as the now denigrated INI file or Windows Registry. As I was preparing to post this, I chanced upon a submission by Chad Z. Hower a.k.a. Kudzu. Chad�s article accomplishes the same task using a different method, and I encourage you to read it. The process I present to you is less tightly coupled to your container class than Chad�s, but also less powerful in handling non-textual data.

Using the code

The demo project consists of three containers, a form and a tab control with two pages. Each container creates its own XML file in the same directory as the executable. To use the code, include the namespaces PFM_FormStateData and PFM_Wrap_Ctrl_Value_Property in your project. There are two ways to save state data and one process to restore state data:

  1. The first method shown below is the easiest to use because it works for all (text-like) controls in the container. As new controls are added to the container, they become part of the solution set, by default.
  2. The second method allows greater programmer control but a little more effort to implement. The second method requires the programmer to be explicit about what controls are part of the solution set.
  3. Lastly, the third segment of code shows how to restore the XML file contents to the controls on the container.
// all code taken form the demo project form.cs

//Method 1 of 2. 

//SAVE defaults from FORM for use next 

//time the application is run.

Control.ControlCollection allControlHeldByThisContainer = this.Controls;
//all controls on this form are candidates for save _

//except those enrolled in 'exceptionArray'.


//a list of controls not to save defaults for, could be null

Control[] exceptionArray = new Control[2];
//but for this example, we will not save radio1 and radio3

exceptionArray[0] = radioButton1;
exceptionArray[1] = radioButton3;

//used to give each container its own file on disk. 

//The container name makes a good default.

string sContainerName = this.Name.ToString();
//a comment to help document the resulting xml file. 

//The container title makes a good default.

string sTitle = this.Text;
SaveRestoreControlDefaults frmDfts = 
    new SaveRestoreControlDefaults(
                      SaveRestoreControlDefaults.eIO.save,
                      allControlHeldByThisContainer,
                      exceptionArray,
                      sContainerName,
                      sTitle);
                      
//------------------------------------------------------------------


//Method 2 of 2.

//In the prior example, the assumption was that 

//defaults where to be saved for most or all controls.

//In this example defaults will only be saved 

//for an explicit few controls.

//In method 1 of 2 there is an assumed 

//relationship between labels and controls.

//In this method, an explicit list 

//of label[0] / control[1] must be setup by you the programmer.

//SAVE default for just ONE control on 

//TAB PAGE 2 for use next time the application is run.

Control[] desiredCtrls = new Control[2];
//always include the controls label

desiredCtrls[0] = this.lblCheckListBox;
desiredCtrls[1] = this.checkedListBox1;
//...


//name of this container to be used in xml file name

string sContainerName = this.tabPage2.Name.ToString();
//a comment to help document the resulting xml file.

string sTitle = "Controls from tabpage2";
SaveRestoreControlDefaults tabDfts = new SaveRestoreControlDefaults();
tabDfts.ExplicitySaveControls(desiredCtrls, sContainerName, sTitle);

//--------------------------------------------------------------------

//RESTORE defaults to the FORM from the last time application was run.

//Loading controls is always done the same way. 

//Loading does not have options like saving.

SaveRestoreControlDefaults frmDfts = new SaveRestoreControlDefaults(
     SaveRestoreControlDefaults.eIO.load, //loading defaults flag

     this.Controls, //controls on a container form

     null, //a list of controls to ignore - in this example none.

     this.Name.ToString(), //name of this container used 

                           //when the xml file was created.

     null); //a comment added to the top node 

            //of the xml file - only used when being saved

Points of interest

That is it. The process of capturing and restoring information to your container is as loosely coupled as I could imagine it. It is only fair to tell you I only dabble in C# occasionally, but I have a lot of C++ experience, and I think this is a utility worth sharing.

Lastly, no part of this code was plagiarized, and you can use it as is or modify it without any restrictions. Hell, you can even put your name on it.

History

VB.NET source is not currently available.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here