Click here to Skip to main content
Click here to Skip to main content

Saving Tree Structure using Serialization

, 22 Jun 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
This article demonstrates editing a tree structure and saving the job using serialization.

Contents

ScreenShot.JPG

Introduction

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

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.

Background

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 TreeNode class.

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.

A 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.

  • Name (Type: string)
  • Text (Type: string)
  • Tag (Type: object)
  • ToolTipText (Type: string)
  • ImageIndex (Type: int)
  • SelectedImageIndex (Type: int)
  • Nodes (Type: List of TreeNode)

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 TreeView's Nodes property. Unfortunately, the TreeNode property is not serializable so there are two ways to achieve the target:

  1. Create a wrapper class that is serializable and can hold the TreeView's node list.
  2. Create a proxy class that is serializable and can hold the TreeNodes' (contained in the TreeView's 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:

  • STreeNode
  • A serializable class that carries all of the above stated properties. This is a proxy class for the non serializable TreeNode class.

Properties

  • Name
  • Text
  • Tag
  • ToolTipText
  • ImageIndex
  • SelectedImageIndex
  • Nodes

Methods

There are no methods in this class.

Constructors

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.

Properties

There are no properties in this class.

Methods

  • 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

The 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

//Declare the binary formatter
BinaryFormatter bin = new BinaryFormatter();
//Get the parent STreeNode object (strToBeGone) that replicates the TreeView
STreeNode strToBeGone = STROperation.fnPrepareToWrite(this.theTree);//theTree is the 
					//TreeView whose structure is to be saved
// Create the file stream object
FileStream fTree = new FileStream(fileName, FileMode.Create, FileAccess.Write);
//Updating in the file (serialize)
bin.Serialize(fTree, strToBeGone);
fTree.Close();

Using Deserialization to Retrieve the Serialized TreeStructure

//Declare the binary formatter
BinaryFormatter bin = new BinaryFormatter();
//Clear the tree view, prepare it to be populated by the 
//TreeNodes that will be retrieved from the serialized file.
this.theTree.Nodes.Clear();
FileStream fTree = new FileStream(fileName, FileMode.Open, FileAccess.Read);
//The STreeNode object that depicts the tree view. 
//The nodes contained in the STreeNode are the replica of the
// TreeView Nodes.
STreeNode str = (STreeNode)bin.Deserialize(fTree);
//Closing the file stream
fTree.Close();
//Populate the TreeView with the TreeNodes converted from the STreeNode objects.
TreeNode trParent = STROperation.fnPrepareToRead(str);//trParent is the tree node 
					//containing all the TreeView Nodes.
foreach (TreeNode trn in trParent.Nodes)
{
    this.theTree.Nodes.Add(trn);
}

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.
  • The 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.

History

  • 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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Siddhartha S.
Software Developer (Senior)
India India
Sid loves programming and has technical experience developing desktop based solutions using C#.Net, Winforms, WPF.
He has experience of Software services, I-Banks and Product development environments.
He also has a deep understanding of Product Development Lifecycle and Agile methodology of developing softwares.
 
Besides programming he is also fond of music, photography and cooking.
He lives in Mumbai with his wife.
Follow on   LinkedIn

Comments and Discussions

 
GeneralMy vote of 5 PinmemberAhmed.ganainy6-Dec-12 0:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.141022.1 | Last Updated 22 Jun 2009
Article Copyright 2009 by Siddhartha S.
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid