Click here to Skip to main content
15,893,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey! How can I store a treeview nodes text properties in a file and retrieve them back later in the same treeview format as it was created? Is it possible to save it in linear format in a text file and then retrive it somehow?
Posted

The best approach is based on the isolation of the UI from the other parts of the application. You don't want to state and restore the state directly. You need to have a separate data model agnostic to your UI, but the data model should be exposed to UI. This way, you will create a loose-coupling approach:
http://en.wikipedia.org/wiki/Loose_coupling[^].

You UI should be able to populate from the model, modify the model or store modified data to the model. As to the persistence of the state, you should have persistence based on the model, not the UI.

The persistent mechanism should be agnostic of the particular data schema (metadata), to be re-usable and reliable. To get such thing, you need to learn about serialization:
http://en.wikipedia.org/wiki/Serialization#.NET_Framework[^],
http://msdn.microsoft.com/en-us/library/ms233843.aspx[^].

The good serialization code should be able to store/restore any arbitrary object graph and be non-intrusive to the data model. It means that the data model should be developed in a way agnostic to serialization mechanism.

The perfect tool for such thing is Data Contract. Please see:
http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

Please also see my past answers where I advocate this approach. You don't have to change anything in your data, you only add some .NET attributes. Please see:
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating property files...[^],
deseralize a json string array[^].

See also:
http://en.wikipedia.org/wiki/Finite-state_machine[^],
http://en.wikipedia.org/wiki/State_diagram[^].

—SA
 
Share this answer
 
Comments
cs101000 15-Oct-12 5:11am    
Thank you very much Sergey for your comprehensive answer and provided links. I got the general concept of serialization but I think I 'll be having issues with implementing it. Can I ask more about it during the implementation process?
Sergey Alexandrovich Kryukov 15-Oct-12 10:40am    
I will. With DataContract, you might have minimal issues. Basically, you

1)just create some data classes, apply [DataContract] attribute to each.

2) Decide which members like fields or properties to persist. Mark them with [DataMember]. Those members become part of contract; you should not remove them in next version

3) To express collection of instances of some class, use nearly any collection, usually System.Collections.Generic<composedType>

4) Use DataContractSerializer to store or load the whole model at once.

--SA
Sergey Alexandrovich Kryukov 15-Oct-12 10:40am    
You are welcome.
Good luck, call again.
--SA

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900