Save, read treeView using class

using System; using System.Windows.Forms; using System.Xml; public class TreeViewSerializer { public TreeViewSerializer() { } public TreeViewSerializer(TreeView treeView) { this.TreeView = treeView; } public TreeView TreeView { get; set; } public void Serialize(string fileName) { if (fileName == null) throw new ArgumentNullException("fileName"); if (this.TreeView == null) throw new NullReferenceException("TreeView can not be null"); XmlDocument document = new XmlDocument(); var declaration = document.CreateXmlDeclaration("1.0", "utf-8", null); document.AppendChild(declaration); var root = document.CreateElement("treeview"); document.AppendChild(root); for(int i = 0; i < this.TreeView.Nodes.Count; i++) root.AppendChild(SerializeNode(this.TreeView.Nodes[i], root, document)); document.Save(fileName); } public void Deserialize(string fileName) { if (fileName == null) throw new ArgumentNullException("fileName"); this.TreeView.Nodes.Clear(); XmlDocument document = new XmlDocument(); document.Load(fileName); var root = document.DocumentElement; for(int i = 0; i < root.ChildNodes.Count; i++) TreeView.Nodes.Add(DeseializeNode(root.ChildNodes[i])); } private XmlElement SerializeNode(TreeNode node, XmlElement root, XmlDocument document) { XmlElement elem = document.CreateElement("node"); XmlAttribute attr = document.CreateAttribute("tag"); XmlAttribute val = document.CreateAttribute("val"); val.InnerText = node.Text; attr.InnerText = node.Tag == null ? "" : node.Tag.ToString(); elem.Attributes.Append(attr); elem.Attributes.Append(val); for(int i = 0; i < node.Nodes.Count; i++) elem.AppendChild(SerializeNode(node.Nodes[i], elem, document)); return elem; } private TreeNode DeseializeNode(XmlNode element) { TreeNode treeNode = new TreeNode(); treeNode.Text = element.Attributes["val"].Value; treeNode.Tag = element.Attributes["tag"].Value; for(int i = 0; i < element.ChildNodes.Count; i++) treeNode.Nodes.Add(DeseializeNode(element.ChildNodes[i])); return treeNode; } } 

The treeView performs the following actions:
- adding nodes: parental, child;
- removal of nodes;
- move nodes (left, right, up, down).

Questions
1. After each action, you need to save ALL the tree or can you somehow save only the changed parts or a comparison with the data source?

or
2. Is it just that at the end of the session (before closing the form) you need to save the ALL tree or can you somehow save only the changed parts or a comparison with the data source?

or
3. It is necessary to save only the changed parts, and for this it is better to use other data sources?
For example DB.

  • How many nodes will you have in the tree: maximum a few hundred. The amount of data, respectively, is small. Therefore, the easiest way to write and read the entire file. If you implement diffs, you will need to first read the main file, then in turn all the files with changes to roll ... Too difficult. And the xml in which you made the storage does not imply a change to the file itself, only a complete rewrite. If you store in the database - then you can update only the modified nodes. But is it really necessary? - Alexander Petrov
  • @AlexanderPetrov Если хранить в БД - тогда можно делать update только изменённых узлов. Но оно действительно надо? Если хранить в БД - тогда можно делать update только изменённых узлов. Но оно действительно надо? Is the most common way to store a tree in XML? Or depends on the situation? And the principle of work: "made changes" -> "saved to XML"? I understand that the question is too general, so I apologize ... I wanted to know your experience ... - eusataf
  • one
    You can store in any format. Which one is more convenient for you - use one. Although binary, at least json ... Push away from the size of the data. If they are not very many - use the file. Many and need to change the individual parts - use the database. principle of operation: "made changes" -> "saved - if changes cannot be lost (even in the case of, for example, a power outage), then yes, we save after each change. If changes are not critical, you can save once, at the end of the application. - Alexander Petrov

0