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.
Если хранить в БД - тогда можно делать 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