private void WriteNodes(StreamWriter sw, TreeNodeCollection treeNodeCollection, Int32 ParentID)
        {
            foreach (TreeNode n in treeNodeCollection)
            {
                CurrentNodeID++;
                NodeTag ntold = (NodeTag)n.Tag;
                NodeTag nt = new NodeTag(CurrentNodeID, ParentID, ntold.ItemID, ntold.CSROnly);
                StringBuilder sb = new StringBuilder();
                sb.Append(nt.ID);
                sb.Append(",");
                sb.Append(nt.ParentID);
                sb.Append(",");
                sb.Append(n.Text);
                sb.Append(",");
                sb.Append(nt.ItemID);
                sb.Append(",");
                sb.Append(n.Checked ? 1:0);
                sb.Append(",");
                sb.Append(nt.CSROnly ? 1 : 0);
                if(CurrentNodeID>1)
                    sw.WriteLine(sb.ToString());

                if (n.Nodes.Count > 0)
                    WriteNodes(sw, n.Nodes, CurrentNodeID);
            }
        }
        private void LoadNodes(String filename)
        {
            StreamReader sr = null;
            try
            {
                sr = new StreamReader(filename);
            }
            catch (Exception Exception){ MessageBox.Show("Problem opening file: \n" + Exception.Message); return; }

            if (sr == null) return;

            ClearNodes();

            String temp = sr.ReadToEnd();
            sr.Close();

            String[] nodes = temp.Split(new char[] { '\n' });
            foreach (string s in nodes)
            {
                String[] data = s.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                if (data.Length == 0) continue;
                if (data.Length != 6)
                {
                    MessageBox.Show("Problem parsing line: " + s);
                    continue;
                }

                NodeTag tag = new NodeTag();

                tag.ID = Convert.ToInt32(data[0]);
                tag.ParentID = Convert.ToInt32(data[1]);
                TreeNode node = new TreeNode(data[2]);
                tag.ItemID = Convert.ToInt32(data[3]);
                node.Checked = Convert.ToInt32(data[4]) == 1;
                tag.CSROnly = Convert.ToInt32(data[5]) == 1;

                node.Tag = tag;
                TreeNode parent = GetNode(treeView1.Nodes, tag.ParentID);
                if (parent != null)
                    parent.Nodes.Add(node);
                else
                    treeView1.Nodes[0].Nodes.Add(node);
            }
        }