Click here to Skip to main content
15,891,136 members
Articles / Desktop Programming / Windows Forms

Saving Tree Structure using Serialization

Rate me:
Please Sign up or sign in to vote.
4.60/5 (4 votes)
22 Jun 2009CPOL5 min read 53.2K   1.7K   32  
This article demonstrates editing a tree structure and saving the job using serialization.
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace ABSTreeSaver
{
    #region  DISCLAIMER
    /* THE CLASS IS PROVIDED "AS IS". ANY ECONOMIC (OR OTHERWISE) 
    /* LOSS/DAMAGE INCURRED DIRECTLY OR INDIRECTLY BECAUSE OF THIS 
    /* CLASS IS TO BE BEARED BY THE USER. THE AUTHOR IS IN NO WAY 
    /* RESPONSIBLE FOR ANY SUCH LOSS/DAMAGE.*/
    #endregion
    public class STROperation
    {
        /// <summary>
        /// This function prepares an StreeNode Similar to the treeView
        /// </summary>
        public static STreeNode fnPrepareToWrite(TreeView treeView)
        {
            try
            {
                TreeNode treeNode = new TreeNode();
                foreach (TreeNode tr in treeView.Nodes)
                {
                    treeNode.Nodes.Add((TreeNode)tr.Clone());
                }
                STreeNode FinalStr = fnPrepareTreeNode(treeNode);
                return FinalStr;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #region Allied functions for writing purpose
        /// <summary>
        /// One of the two recursives.
        /// </summary>
        /// <param name="tr"></param>
        /// <returns></returns>
        private static List<STreeNode> fnPrepareChildNode(TreeNode tr)
        {
            List<STreeNode> retSTreeNode = new List<STreeNode>();
            STreeNode stc;
            foreach (TreeNode trc in tr.Nodes)
            {
                stc = fnPrepareTreeNode(trc);
                retSTreeNode.Add(stc);
            }
            return retSTreeNode;
        }
        /// <summary>
        /// One of the two recursives
        /// </summary>
        /// <param name="tr"></param>
        /// <returns></returns>
        private static STreeNode fnPrepareTreeNode(TreeNode tr)
        {
            STreeNode strRet = new STreeNode();
            strRet.Name = tr.Name;
            strRet.ToolTipText = tr.ToolTipText;
            strRet.ImageIndex = tr.ImageIndex;
            if (tr.Tag != null)
                strRet.Tag = tr.Tag.ToString();
            else
                strRet.Tag = null;
            strRet.Text = tr.Text.ToString();
            strRet.SelectedImageIndex = tr.SelectedImageIndex;
            strRet.Nodes = fnPrepareChildNode(tr);
            return strRet;
        }
        #endregion
        /// <summary>
        /// This functions returns the treeView for which it has been written
        /// </summary>
        /// <returns></returns>
        public static TreeNode fnPrepareToRead(STreeNode sTreeNode)
        {
            try
            {
                TreeNode FinalTreeNode = RfnPrepareTreeNode(sTreeNode);
                return FinalTreeNode;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #region Allied functions for Writing Purpose
        private static List<TreeNode> RfnPrepareChildNodes(STreeNode str)
        {
            List<TreeNode> retTreeNode = new List<TreeNode>();
            TreeNode tnc;
            foreach(STreeNode strc in str.Nodes)
            {
                tnc = RfnPrepareTreeNode(strc);
                retTreeNode.Add(tnc);
            }
            return retTreeNode;
        }
        private static TreeNode RfnPrepareTreeNode(STreeNode str)
        {
            TreeNode trnRet = new TreeNode();
            trnRet.Name = str.Name;
            trnRet.Tag = str.Tag;
            trnRet.ToolTipText = str.ToolTipText;
            trnRet.ImageIndex = str.ImageIndex;
            trnRet.Text = str.Text;
            trnRet.SelectedImageIndex = str.SelectedImageIndex;
            #region Building NodeCollection
            List<TreeNode> retTempTreeNodeList = RfnPrepareChildNodes(str);
            foreach (TreeNode tempTr in retTempTreeNodeList)
            {
                trnRet.Nodes.Add(tempTr);
            }
            #endregion
            return trnRet;
        }
        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
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 Bangalore.

Comments and Discussions