Click here to Skip to main content
15,911,786 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have this class (a recursive definition of a tree) which I wana provide a save/load mechanism for it(on an xml file or sql server database). What is the best way to do this?

C#
public class TaskViewModel : INotifyPropertyChanged
{
    private Task vmTask;    
    private ObservableCollection<TaskViewModel> vmSubTasks;
    TaskViewModel vmParent;
    bool vmIsExpanded;
    bool vmIsSelected;
    public bool VmIsVisited { get; set; }

    //ctors
    public TaskViewModel(Task task, TaskViewModel prnt)
    {
        VmTask = task;  //:Me vmTask = task;
        Parent = prnt; //:Me vmParent = parent; 
        ///<summary>
        ///recursively walks down the Task tree, wrapping each Task object in a TaskViewModel
        ///</summary>
        SubTasks = new ObservableCollection<TaskViewModel>( //:Me vmSubTasks = new ObservableCollection<TaskViewModel>(
                (from t in vmTask.SubTasks
                 select new TaskViewModel(t, this)).ToList<TaskViewModel>());          
    }
    public TaskViewModel(Task task)
        : this(task, null)
    { }
    public bool IsExpanded
    {
        get { return vmIsExpanded; }
        set
        {
            if (value != vmIsExpanded)
            {
                vmIsExpanded = value;
                this.OnPropertyChanged("IsExpanded");
            }
            // Expand all the way up to the root.
            if (vmIsExpanded && vmParent != null)
                Parent.IsExpanded = true;   //fantastic! (executes the setter again)
        }
    }
    public bool IsSelected
    {
        get { return vmIsSelected; }
        set
        {
            if (value != vmIsSelected)
            {
                vmIsSelected = value;
                this.OnPropertyChanged("IsSelected");
                OnPropertyChanged("Name");  //required: reflects the change of txtName to the corresponding TreeViewItem name  
                //OnPropertyChanged is only required for the Name property
            }
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    } 
}
Posted
Comments
Sergey Alexandrovich Kryukov 23-Oct-12 13:09pm    
Best? Can you explain what's "better" for you? Anyway, one approach pretty much fits all... please see my answer.
--SA
cs101000 25-Oct-12 5:29am    
Problem with serialization! (solution 3)
regards, cs101000
Sergey Alexandrovich Kryukov 25-Oct-12 13:03pm    
I removed your "solution", because it is not a solution. And why did you even accept it formally?!
If you need to add code, use "Improve question".
I suggested what to do in my answer, please try it. It's free from the problem with legacy serialization.
--SA
cs101000 25-Oct-12 16:16pm    
I didnt accept anything
Sergey Alexandrovich Kryukov 25-Oct-12 16:25pm    
I can see it, no need to note... :-)
--SA

The best way of loading and storing object is using Data Contract:
http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

Please see my past answers to similar questions where I advocate this approach:
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating property files...[^],
deseralize a json string array[^].

—SA
 
Share this answer
 
 
Share this answer
 

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