Serialize a tree?

  • Thread starter Thread starter Toxick
  • Start date Start date
T

Toxick

Hello experts,

I'm a total C# noob with a total C# noob question.



I've been Serializing in C++ (MFC) and writing data to std::fstreams for
quite some time, so maybe I'm not understanding this different
technique, or reading too much into it, or not enough: but, I'm not
quite wrapping my brain around serialization in C#.



Here's what I have and here's what I want to do:



I have a [Serializable] abstract class, "Bead". Derived from Bead is a
family of Bead-Types - RedBead, BlueBead, GreenBead, OrangeBead,
ClearBead, etc - all of which are [Serializable].


Over here, I have a TreeView object. Each TreeNode of the TreeView
object has a Bead object assigned to it, via the Tag property.



I want to save the Bead hierarchy as it appears in the tree.



Now, if I were to Serialize the TreeView itself, would it properly save
each attached Bead when it does so? Do I have to make each Bead type
serializable, and define serialize/deserialize methods for them?


I dont mind traversing the tree and having each bead serialize and
deserialize itself, (which was what I was going to do), but I'm
wondering if this "Save the whole darned Tree" approach would work.

Any thoughts?



TIA.
 
You really want to serialize the control?
Is there a reason you wouldn't just save the data?

No, theres no reason, and in fact, I actually had a solution in mind
when I wrote the original post, but if there's something built in to
save a tree in a tree format it'll save me some work.


I'm not so much concerned with saving the control, as I am saving the
data attached to the tag properites of the nodes, and keeping the
*hierarchy* in tact.

I want to store the beads of data in a hierarchical fashion, and the
tree control provides a convenient means of structure, display and
(hopefully) storage. I had actually started to write a Tree object,
which was just a container, but then I realized, duh - the TreeView
already has the structure I need. I'll just use that as the data
container.


But I still need to save the data.


I'm going to read the article you posted - and thank you - but upon an
initial skim, I'm not sure if it explains much more than I already have
done - that is using the tag attribute of the TreeNode objects to pin
my objects to the tree. Which I've done and which works.



Am I to infer from your post that deriving a "BeadTreeView" class from
the TreeView and labelling it [Serializable] and then serializing it
(perhaps with a BinaryFormatter object) will not work.
 
The thing that that article does is keep the DataRow in each .Tag
in sync with the DataSet and the TreeView. If you change the
order in the tree, drag and drop nodes, delete nodes, add nodes, etc...

The class and the DataSet are all kept in sync automatically.

As far as serializing the control? I just didn't see the logic
in trying or a business case for doing so.

--
Robbe Morris - 2004/2005 Microsoft MVP C#

Earn money answering .NET Framework
messageboard posts at EggHeadCafe.com.
http://www.eggheadcafe.com/forums/merit.asp



You really want to serialize the control?
Is there a reason you wouldn't just save the data?

No, theres no reason, and in fact, I actually had a solution in mind
when I wrote the original post, but if there's something built in to
save a tree in a tree format it'll save me some work.


I'm not so much concerned with saving the control, as I am saving the
data attached to the tag properites of the nodes, and keeping the
*hierarchy* in tact.

I want to store the beads of data in a hierarchical fashion, and the
tree control provides a convenient means of structure, display and
(hopefully) storage. I had actually started to write a Tree object,
which was just a container, but then I realized, duh - the TreeView
already has the structure I need. I'll just use that as the data
container.


But I still need to save the data.


I'm going to read the article you posted - and thank you - but upon an
initial skim, I'm not sure if it explains much more than I already have
done - that is using the tag attribute of the TreeNode objects to pin
my objects to the tree. Which I've done and which works.



Am I to infer from your post that deriving a "BeadTreeView" class from
the TreeView and labelling it [Serializable] and then serializing it
(perhaps with a BinaryFormatter object) will not work.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top