Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving content of a treeview to a file and load it later

In my C# WinForms program I have a treeview that only contains parent nodes (so, no childs) it is like a listbox but I needed it because of haveing differet properties of nodes like Name, Tag and Text.

No I want to be able to save the content of this treeview into a file (Basically a text file which I call it *.MVIA). The question is what is the best way to save all three properties of nodes in a file so it can loaded again later properly?

At the moment I came with this idea :

    private void menuFileSave_Click(object sender, EventArgs e)
    {
        StringBuilder sb = new StringBuilder();

        foreach(TreeNode node in treeViewFiles.Nodes)
        {
            sb.AppendLine(node.Name);
        }

        SaveFileDialog saveList = new SaveFileDialog();

        saveList.DefaultExt = "*.mvia";
        saveList.Filter = "MVIA Files|*.mvia";

        if (saveList.ShowDialog() == DialogResult.OK)
        {
            File.WriteAllText(saveList.FileName, sb.ToString());
        }            
    }

As you can see, each Name property of each node will be saved in a line. Now I need to add its Text and Tag property also, but later I have trouble reading it back (Honestly I don't know how to).

Would you give me some ideas what is a best way to save all three property of each node and be able to load it easily later?

Thanks.

like image 492
Saeid Yazdani Avatar asked May 03 '11 11:05

Saeid Yazdani


People also ask

How do I get data from TreeView database?

Populating TreeView from database Inside the PopulateTreeView method, a loop is executed over the DataTable and if the ParentId is 0 i.e. the node is a parent node, a query is executed over the VehicleSubTypes table to populate the corresponding child nodes and again the PopulateTreeView method is called.

How do I create a folder in TreeView?

Combine(treeView1. SelectedNode. Tag as string, "New Folder")); This will create a directory which is represented by the combination of the path of the current node and the string 'New Folder'.

How do you bind TreeView?

You can bind a TreeView control to an XML file by creating an XmlDataSource control that represents the XML file and then assigning that XmlDataSource to your TreeView control.


1 Answers

You can use BinaryFormatter to Serialize/Deserialize Nodes

    public static void SaveTree(TreeView tree, string filename)
    {
        using (Stream file = File.Open(filename, FileMode.Create))
        {
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(file, tree.Nodes.Cast<TreeNode>().ToList());
        }
    }

    public static void LoadTree(TreeView tree, string filename)
    {
        using (Stream file = File.Open(filename, FileMode.Open))
        {
            BinaryFormatter bf = new BinaryFormatter();
            object obj = bf.Deserialize(file);

            TreeNode [] nodeList = (obj as IEnumerable<TreeNode>).ToArray();
            tree.Nodes.AddRange(nodeList);
        }
    }
like image 161
Stecya Avatar answered Oct 14 '22 14:10

Stecya