Click here to Skip to main content
13,353,462 members (40,423 online)
Click here to Skip to main content
Add your own
alternative version


12 bookmarked
Posted 20 Mar 2008

How to Serialize a TreeNode Object

, 20 Mar 2008
Rate this:
Please Sign up or sign in to vote.
To serialize a TreeNode derived class is not easy, but it can be done


As serializing of a TreeNode object does not work as it is said in the Microsoft’s online page, or other Web pages, e.g. Object Serialization using C# on this Web site.

If you try to write a class inherited from a TreeNode object, you will find a NullPointerException during serialization to/from the file. Your process crashes, and a wonderful and beautiful NullPointerException is shown (if you've not caught it).

The reason is one, simple and not easy to reach: the TreeNode object implements the ISerializable interface but deserialization does not work very well, that's because you cannot simply serialize an object that inherits the TreeNode and be able to [Serialize] it.

You can find an example of the problem here. I did not write that code, but it's a great example, and in this article you can find how to resolve it.

So, if you try to write code like this:

public class MyObject : TreeNode
public MyObject(string dirName) : base(dirName)

You will find that, one of the most simple code ever made will crash when you try to serialize and de-serialize the MyObject object.

static void Main(string[] args)
MyObject obj = new MyObject("blabla");
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("MyFile.bin", FileMode.Create,
FileAccess.Write, FileShare.None);
formatter.Serialize(stream, obj);

Don't worry, there is a SIMPLE and EASY solution, but it would be easier if [Serialize] tag worked as it was supposed to (like Java). I think it's not necessarily a full project to show how it can be done, it's really very simple!


I surfed the Internet, spending a lot of hours trying to find the answer for this problem in C#. Don’t worry, it does not exist anywhere but here. I found some objects that used TreeNode and TreeView as a base class, and derived it (and you had to derive that derived class, not very practical). I suggest that you inherit the TreeNode class, because the TreeView class is used only for GUI approaches (I must say it: use the Facade pattern, very easy and useful while using a memory-GUI duet).

But it was not what I wanted, and it was a lot of work to change my project, and mapping all my work to store it in an XML (so cool, so fantastic, …) but I wanted binary files.

What I wanted was to derive a TreeNode, and store it to a binary or XML file, does not matter what the structure of my object was. At Microsoft's Web page was no answer, I tried to find it. And I won.

Using the Code

After a long time, I found the answer: you can derive your class from TreeNode, but the serializing way works different from other classes (not to say Microsoft’s help). This is how I did it:

First of all, derive your class:

public class DbVisioFile: TreeNode, …
    private string mFilePath;
    private string mOwner;
    private string mSummary;
    private string mDifferences;
    private string mComments;
    private Version mVersion;

After this, make your own Serialization control-code:

#region Serialization control
protected DbVisioFile(SerializationInfo si, StreamingContext context)
: base(si, context)
    this.FilePath = si.GetString("FilePath");
    this.Owner = si.GetString("Owner");
    this.Summary = si.GetString("Summary");
    this.Differences = si.GetString("Differences");
    this.Comments = si.GetString("Comments");
    this.Version = new Version(si.GetString("Version"));

protected override void Serialize(SerializationInfo si, StreamingContext context)
    base.Serialize(si, context);
    si.AddValue("FilePath", this.FilePath);
    si.AddValue("Owner", this.Owner);
    si.AddValue("Summary", this.Summary);
    si.AddValue("Differences", this.Differences);
    si.AddValue("Comments", this.Comments);
    si.AddValue("Version", this.Version.ToString());

You can see that the code has no reason to work, but it does. It is a mixture of the ISerializable and [Serializable] point of view.

Points of Interest

In the overriding of the Serialize method, the first line is:

base.Serialize(si, context);

As a result of this, the treenode structure is serialized to disk.

You have to be very careful with calling the following line in the constructor code.

:base(si, context) 

If It Was Useful To You...

... or if you have any questions, please send me an email at with a clear question and the source code.

Remember, the beginnings are very hard. And if you work with a language that doesn't work as it is supposed to, it is even harder.

Sorry for my English

So sorry for my English, but if you are English, try to rewrite it in Spanish and send me. We both will have some laughs!


  • 20th March, 2008: Initial post


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


About the Author

Instructor / Trainer
Spain Spain
Young and handsome code-writer, born and living in Spain, left the SW developer job to a very wonderful life of teaching other how to waste your life in front of a 20" machine.
I don't like to write code for other, but code & programs that I write for me, that's great!

You may also be interested in...

Comments and Discussions

Questionthanks you very much Pin
Member 805185210-Aug-11 8:41
memberMember 805185210-Aug-11 8:41 
AnswerRe: thanks you very much Pin
Victor_Gus17-Sep-11 15:59
memberVictor_Gus17-Sep-11 15:59 
GeneralMy note is 5 + XML serialization question Pin
yuriyag3-Feb-11 0:07
memberyuriyag3-Feb-11 0:07 
GeneralRe: My note is 5 + XML serialization question Pin
yuriyag3-Feb-11 4:16
memberyuriyag3-Feb-11 4:16 
GeneralRe: My note is 5 + XML serialization question Pin
yuriyag3-Feb-11 4:31
memberyuriyag3-Feb-11 4:31 
AnswerFinally I have found a good idea right here, on codeproject! Pin
yuriyag3-Feb-11 7:49
memberyuriyag3-Feb-11 7:49 
GeneralRe: Finally I have found a good idea right here, on codeproject! Pin
Victor_Gus1-Aug-11 6:07
memberVictor_Gus1-Aug-11 6:07 
GeneralExcellent Article, thank you Pin
dvanderwerken24-Sep-09 11:03
memberdvanderwerken24-Sep-09 11:03 
GeneralMany Thanks Pin
kal7hos31-May-09 13:29
memberkal7hos31-May-09 13:29 
GeneralRe: Many Thanks Pin
Victor_Gus17-Sep-11 15:51
memberVictor_Gus17-Sep-11 15:51 
GeneralOK, that's better Pin
PIEBALDconsult22-Mar-08 7:40
memberPIEBALDconsult22-Mar-08 7:40 
GeneralRe: OK, that's better Pin
Victor_Gus22-Mar-08 14:46
memberVictor_Gus22-Mar-08 14:46 
GeneralRe: OK, that's better Pin
PIEBALDconsult22-Mar-08 16:57
memberPIEBALDconsult22-Mar-08 16:57 
AnswerRe: OK, that's better Pin
Victor_Gus23-Mar-08 2:44
memberVictor_Gus23-Mar-08 2:44 
GeneralRe: OK, that's better Pin
PIEBALDconsult23-Mar-08 5:53
memberPIEBALDconsult23-Mar-08 5:53 
GeneralRe: OK, that's better Pin
Victor_Gus23-Mar-08 12:18
memberVictor_Gus23-Mar-08 12:18 
Generalif [Serialize] tag worked as supposed to (like Java). Pin
Derek Bartram21-Mar-08 16:47
memberDerek Bartram21-Mar-08 16:47 
GeneralRe: if [Serialize] tag worked as supposed to (like Java). Pin
Victor_Gus22-Mar-08 6:59
memberVictor_Gus22-Mar-08 6:59 
GeneralRe: if [Serialize] tag worked as supposed to (like Java). Pin
Derek Bartram22-Mar-08 7:37
memberDerek Bartram22-Mar-08 7:37 
GeneralRe: if [Serialize] tag worked as supposed to (like Java). Pin
Victor_Gus1-Aug-11 6:08
memberVictor_Gus1-Aug-11 6:08 
Wow, that's very interesting a clever explanation!
Java expert, .NET beginner Frown | :(

GeneralPlease explain Pin
PIEBALDconsult21-Mar-08 5:43
memberPIEBALDconsult21-Mar-08 5:43 
GeneralRe: Please explain Pin
Victor_Gus22-Mar-08 6:58
memberVictor_Gus22-Mar-08 6:58 
GeneralSomething I have never seen... Pin
Justin Perez20-Mar-08 4:18
memberJustin Perez20-Mar-08 4:18 
JokeRe: Something I have never seen... Pin
Victor_Gus20-Mar-08 4:36
memberVictor_Gus20-Mar-08 4:36 
GeneralRe: Something I have never seen... Pin
Justin Perez20-Mar-08 4:37
memberJustin Perez20-Mar-08 4:37 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180111.1 | Last Updated 20 Mar 2008
Article Copyright 2008 by Victor_Gus
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid