Sign in to follow this  
Krisc

[.net] Serializing Data In a TreeView

Recommended Posts

I have data in a TreeView with custom node classes, what is the best way to serialize/deserialize to/from XML? Should I just build it into my custom node classes? A little more information. Each class inherits from System.Windows.Forms.TreeNode. Serializing the data the normal way throws an exception stating that the type cannot be reflected. Here are the classes. BaseNode - Inherits from TreeNode.
#region Using Statements
using System;
using System.Xml;
using System.Xml.Schema;
using System.Windows.Forms;
using System.Xml.Serialization;

#endregion

namespace AssetStudio.Data
{
    [Serializable]
    public class BaseNode : TreeNode
    {
        private int m_closedImageIndex = 0;
        private int m_expandedImageIndex = 0;

        private bool m_canEdit = true;

        #region Constructor
        public BaseNode()
        {
        }

        public BaseNode(string text)
            : base(text)
        {
        }
        #endregion

        public new void BeginEdit()
        {
            if(m_canEdit)
                base.BeginEdit();
        }

        #region Image Methods
        public void ChangeImage(Images imageIndex)
        {
            this.SelectedImageIndex = (int)imageIndex;
            this.ImageIndex = (int)imageIndex;
        }

        public void ChangeImage(int imageIndex)
        {
            this.SelectedImageIndex = imageIndex;
            this.ImageIndex = imageIndex;
        }

        public void UpdateImage()
        {
            if(this.IsExpanded)
                ChangeImage(m_expandedImageIndex);
            else 
                ChangeImage(m_closedImageIndex);
        }
        #endregion

        #region Properties
        protected int ClosedImageIndex
        {
            get { return m_closedImageIndex; }
            set { m_closedImageIndex = value; }
        }

        protected int ExpandedImageIndex
        {
            get { return m_expandedImageIndex; }
            set { m_expandedImageIndex = value; }
        }

        public bool CanEdit
        {
            get { return m_canEdit; }
            set { m_canEdit = value; }
        }

        public new string Name
        {
            get { return base.Text; }
            set { base.Text = value; }
        }
        #endregion
    }
}
FolderNode
#region Using Statements
using System;
using System.IO;
using System.Windows.Forms;
#endregion

namespace AssetStudio.Data
{
    public class FolderNode : BaseNode
    {
        #region Constructor & Initialization
        public FolderNode()
        {
            Initialize();
        }

        public FolderNode(string text)
            : base(text)
        {
            Initialize();
        }

        private void Initialize()
        {
            this.ContextMenuStrip = frmMain.FolderMenu;

            this.ExpandedImageIndex = (int)Images.FolderOpen;
            this.ClosedImageIndex = (int)Images.FolderClosed;
        }
        #endregion
    }
}

AssetNode
#region Using Statements
using System;
using System.Windows.Forms;
#endregion

namespace AssetStudio.Data
{
    public class AssetNode : BaseNode
    {
        #region Constructors
        public AssetNode()
        {
        }

        public AssetNode(string text)
            : base(text)
        {
        }
        #endregion
    }
}
[Edited by - Krisc on August 3, 2007 10:55:05 AM]

Share this post


Link to post
Share on other sites
What you are most likely going to want to do is create your own TreeView that inherits from TreeView, then you are going to want to also add the ISerializable interface.


public SkillInformation(SerializationInfo info, StreamingContext ctxt)
{
// this is the Serialization contructor
}
new public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
{
// This is the custom Serializer.
}



In a lot of cases you have to control your own serialization. If you need more information, let me know.

theTroll

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this