I had always wondered how stand alone applications store information of a job in files. e.g. Microsoft Word does in a .doc(x) file, Microsoft Excel does in a .xls(x) file. Then I happened to learn about serialization. As I delved more and more into it, I realized serialization has a much bigger use than a mere storage job. However, in this article, I am just taking up the use of serialization as a job storer.
Just like I had put it in my last article:
On a personal note: I am not much of a professional programmer nor am I backed with years of experience. I haven't written the article with the view of teaching a new concept altogether. For that, there are some real cool guys out here. I consider myself a little above beginners’ level. My aim is just to share something that may save time for some developers and thus contribute to this wonderful community here. The code presented may not be optimum; I sincerely invite your comments so that I may keep the article updated.
Serialization is a technique which allows storage and/or transfer of data stored in objects. .NET Framework includes several serialization techniques:
- Binary Serialization
- SOAP Serialization
- XML Serialization
Binary serialization is the most preferred one for the case presented here because of the following reasons:
- It is secure. When I say secure it does not mean that cracking the data is a hard task. It's just that binary data, if viewed as text is not an easy(and pleasant) read. To understand this, open a Microsoft Word document using Notepad. Data that is serialized using SOAP (Simple object access protocol) serialization, on the contrary, is easy to read (and hence manipulate).
- It is faster.
The beauty of serialization includes defining your own file type. In the project attached with this article, I have kept it '.aks'. However you can always define your own type. Open the tree1.aks (provided as a sample) file kept in the debug folder of the
HostApp using the application attached. This is a nice link demonstrating object serialization (the same thing we are going to use). Unless you are new to Windows Programming in .NET, you may skip the below section and directly jump to Knowing the ABSTreeSaver.
In this article, I will demonstrate how you can edit a tree structure and save the job for future use. Before starting, let's get clear about certain properties of a
TreeView control and
TreeView is a control that represents hierarchical structure therefore any data representation that requires Organizational chart type representation is represented using this control, e.g.:
- Staff information in office requires this type of representation. You are under your boss, while your subordinates are under you.
- A family tree. You and your siblings are under your father, while he and his siblings are under your grandpa.
TreeNode represents a node in the
TreeView control. It contains several properties. We, however would limit our interest to the
TreeNode properties that are inevitable.
Nodes (Type: List of
The above list presents inevitable (and not exclusive) list of
Treenode properties that you would need to be restored in a job (file). We therefore need to
serialize a list of
TreeNodes contained in the
Nodes property. Unfortunately, the
TreeNode property is not serializable so there are two ways to achieve the target:
- Create a wrapper class that is serializable and can hold the
TreeView's node list.
- Create a proxy class that is serializable and can hold the
TreeNodes' (contained in the
Nodes property) essential properties.
The article displays the second way that is undoubtedly the longer one. In the second version of this article, I plan to include the first way too.
Knowing the ABSTreeSaver
Considering that the code has been downloaded and the DLL ABSTreeSaver.dll has been procured. Once the reference to the DLL has been added in your application, you will have access to the following two classes:
- A serializable class that carries all of the above stated properties. This is a proxy class for the non serializable
There are no methods in this class.
I have only added the default constructor. The code however can be modified to include a constructor that would initialize the properties.
STROperation - This is the class responsible for reading/writing of
STreeNode objects from/to files.
There are no properties in this class.
public static STreeNode fnPrepareToWrite(TreeView treeView)
This method prepares an
StreeNode similar to the
TreeNode however this class would be serializable.
public static TreeNode fnPrepareToRead(STreeNode sTreeNode)
This method returns the
TreeNode in whose
Nodes collection the
TreeNodes of the
TreeView will be there.
- There are four other
private methods in this class consumed by these two methods.
Below the Abstraction Layer
STreeNode class is nothing but a serializable clone of the
TreeNode class with limited properties. Here I have demonstrated the class with seven properties. You can always modify the class to add more properties. The
Tree structure that you save, is mapped with
STreeNode objects and serialized. When you open the serialized file, the
STreeNode object is deserialized and mapped back with the
TreeNode objects that gets displayed in the
TreeView. The below sub sections will explain it.
Using Binary Serialization to Store TreeStructure
BinaryFormatter bin = new BinaryFormatter();
STreeNode strToBeGone = STROperation.fnPrepareToWrite(this.theTree); FileStream fTree = new FileStream(fileName, FileMode.Create, FileAccess.Write);
Using Deserialization to Retrieve the Serialized TreeStructure
BinaryFormatter bin = new BinaryFormatter();
FileStream fTree = new FileStream(fileName, FileMode.Open, FileAccess.Read);
STreeNode str = (STreeNode)bin.Deserialize(fTree);
TreeNode trParent = STROperation.fnPrepareToRead(str); foreach (TreeNode trn in trParent.Nodes)
Limitations and Drawbacks
There are quite a few of them:
- Though serialization in itself is an extremely fast operation, the mapping of
STreeNode object into
TreeNode and vice versa is the operation that may hit performance if used to save huge and heavy structure. The mapping is done using recursive calling of efficient methods, yet this way is not recommended for heavy operations.
STreeNode class no matter how much extended can never be like a real
TreeNode. Therefore, it is only for data storing that this method can be used.
- 15th June, 2009: Initial post
- 17th June, 2009: Included heading on Serialization
- 21st June, 2009: Included text on Serialization
- 21st June, 2009: Added code walkthrough of the application