C#treeview添加的节点怎么保存

harukalove 2017-04-23 08:22:50
我是新手尽可能说的详细一点

效果大概是这样的
一个treeview 可以通过textbox和button来添加父节点和子节点
这些我知道怎么写
但是添加的父节点和子节点在重启程序后不会保存

怎么样才能在下次开启软件的时候treeview里保存上次添加的节点?
我看了下通过xml实现?能不能解释的详细一点


下面是追加的问题 不解决也暂时没事
那么这些节点信息怎么样才能保存成本地文件 通过button来进行保存和读取节点的内容?
...全文
599 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
wang_peng_yl 2017-04-26
  • 打赏
  • 举报
回复
引用 7 楼 harukalove 的回复:
[quote=引用 1 楼 wang_peng_yl 的回复:] //---加载节点 private void LoadTreeNode() { System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument(); string filename = AppDomain.CurrentDomain.BaseDirectory + "111.xml"; xmlDoc.Load(filename); if (xmlDoc.ChildNodes.Count > 0) { System.Xml.XmlNodeList nodes = xmlDoc.ChildNodes[0].ChildNodes; this.LoadNode(nodes, this.treeView1.Nodes); } } private void LoadNode(System.Xml.XmlNodeList nodes, TreeNodeCollection pNodes) { foreach (System.Xml.XmlElement element in nodes) { TreeNode node = new TreeNode(); node.Text = element.GetAttribute("Value"); pNodes.Add(node); LoadNode(element.ChildNodes, node.Nodes); } } //---保存节点 private void SaveTreeNode() { System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument(); xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "yes"); System.Xml.XmlElement rootNode = xmlDoc.CreateElement("root"); xmlDoc.AppendChild(rootNode); this.SaveTreeNode(xmlDoc, rootNode, this.treeView1.Nodes); string filename = AppDomain.CurrentDomain.BaseDirectory + "111.xml"; xmlDoc.Save(filename); } private void SaveTreeNode(System.Xml.XmlDocument xmlDoc, System.Xml.XmlElement rootNode, TreeNodeCollection nodes) { foreach (TreeNode node in nodes) { System.Xml.XmlElement pNode = xmlDoc.CreateElement("ChildNode"); pNode.SetAttribute("Value", node.Text); rootNode.AppendChild(pNode); SaveTreeNode(xmlDoc, pNode, node.Nodes); } }
treeview里本身有一些节点的时候 添加节点会添加重复的节点 怎么样只添加不重复的节点名呢 我这么写 foreach (TreeNode n in treeView1.Nodes) { if (node.Text != n.Text) pNodes.Add(node); } 会报错 System.ArgumentException:“不能在多处添加或插入项“节点0”。必须首先将其从当前位置移除或将其克隆。” [/quote] pNodes.Add(node); 中node值哪来的,应该是它加重复了
wang_peng_yl 2017-04-25
  • 打赏
  • 举报
回复
出这个错是因为同一个new treenode()出来的节点加了两次,注意一下别加两次。从你的写法上看,应该是那个node已经加过了,再加时重新new一个就完了
harukalove 2017-04-25
  • 打赏
  • 举报
回复
引用 1 楼 wang_peng_yl 的回复:
//---加载节点 private void LoadTreeNode() { System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument(); string filename = AppDomain.CurrentDomain.BaseDirectory + "111.xml"; xmlDoc.Load(filename); if (xmlDoc.ChildNodes.Count > 0) { System.Xml.XmlNodeList nodes = xmlDoc.ChildNodes[0].ChildNodes; this.LoadNode(nodes, this.treeView1.Nodes); } } private void LoadNode(System.Xml.XmlNodeList nodes, TreeNodeCollection pNodes) { foreach (System.Xml.XmlElement element in nodes) { TreeNode node = new TreeNode(); node.Text = element.GetAttribute("Value"); pNodes.Add(node); LoadNode(element.ChildNodes, node.Nodes); } } //---保存节点 private void SaveTreeNode() { System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument(); xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "yes"); System.Xml.XmlElement rootNode = xmlDoc.CreateElement("root"); xmlDoc.AppendChild(rootNode); this.SaveTreeNode(xmlDoc, rootNode, this.treeView1.Nodes); string filename = AppDomain.CurrentDomain.BaseDirectory + "111.xml"; xmlDoc.Save(filename); } private void SaveTreeNode(System.Xml.XmlDocument xmlDoc, System.Xml.XmlElement rootNode, TreeNodeCollection nodes) { foreach (TreeNode node in nodes) { System.Xml.XmlElement pNode = xmlDoc.CreateElement("ChildNode"); pNode.SetAttribute("Value", node.Text); rootNode.AppendChild(pNode); SaveTreeNode(xmlDoc, pNode, node.Nodes); } }
treeview里本身有一些节点的时候 添加节点会添加重复的节点 怎么样只添加不重复的节点名呢 我这么写 foreach (TreeNode n in treeView1.Nodes) { if (node.Text != n.Text) pNodes.Add(node); } 会报错 System.ArgumentException:“不能在多处添加或插入项“节点0”。必须首先将其从当前位置移除或将其克隆。”
tanta 2017-04-25
  • 打赏
  • 举报
回复
treeview状态的保存和恢复 using System; using System.Data; using System.Configuration; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Collections.Generic; /// <summary> ///TreeViewState 的摘要说明 /// </summary> public class TreeViewState { public void SaveTreeView(TreeView treeView, string key) { List<bool?> list = new List<bool?>(); SaveTreeViewExpandedState(treeView.Nodes, list); HttpContext.Current.Session[key + treeView.ID] = list; } private void SaveTreeViewExpandedState(TreeNodeCollection nodes, List<bool?> list) { foreach (TreeNode node in nodes) { list.Add(node.Expanded); if (node.ChildNodes.Count > 0) { SaveTreeViewExpandedState(node.ChildNodes, list); } } } private int RestoreTreeViewIndex; public void RestoreTreeView(TreeView treeView, string key) { RestoreTreeViewIndex = 0; RestoreTreeViewExpandedState(treeView.Nodes, (List<bool?>)HttpContext.Current.Session[key + treeView.ID] ?? new List<bool?>()); } private void RestoreTreeViewExpandedState(TreeNodeCollection nodes, List<bool?> list) { foreach (TreeNode node in nodes) { if (RestoreTreeViewIndex >= list.Count) return; node.Expanded = list[RestoreTreeViewIndex++]; if (node.ChildNodes.Count > 0) { RestoreTreeViewExpandedState(node.ChildNodes, list); } } } } 在页面代码,TreeView的控件里事件里写: //刷新时,页面还原为原来的信息 protected void TreeView1_Load(object sender, EventArgs e) { new TreeViewState().RestoreTreeView(TreeView1, this.GetType().ToString()); } //点击展开和收缩的时候 保存节点的信息 protected void TreeView1_TreeNodeExpanded(object sender, TreeNodeEventArgs e) { new TreeViewState().SaveTreeView(TreeView1, this.GetType().ToString()); } protected void TreeView1_TreeNodeCollapsed(object sender, TreeNodeEventArgs e) { new TreeViewState().SaveTreeView(TreeView1, this.GetType().ToString()); }
wang_peng_yl 2017-04-24
  • 打赏
  • 举报
回复
引用 2 楼 shoppo0505的回复:
最简单的就是添加的时候直接加进datasource中,然后帮定到treeview,datasource在导出成一个xml文件保存。
c#里树没有这个属性吧。
harukalove 2017-04-24
  • 打赏
  • 举报
回复
谢谢上面的回答 我先消化一下啊
exception92 2017-04-24
  • 打赏
  • 举报
回复
shoppo0505 2017-04-24
  • 打赏
  • 举报
回复
最简单的就是添加的时候直接加进datasource中,然后帮定到treeview,datasource在导出成一个xml文件保存。
wang_peng_yl 2017-04-24
  • 打赏
  • 举报
回复
//---加载节点 private void LoadTreeNode() { System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument(); string filename = AppDomain.CurrentDomain.BaseDirectory + "111.xml"; xmlDoc.Load(filename); if (xmlDoc.ChildNodes.Count > 0) { System.Xml.XmlNodeList nodes = xmlDoc.ChildNodes[0].ChildNodes; this.LoadNode(nodes, this.treeView1.Nodes); } } private void LoadNode(System.Xml.XmlNodeList nodes, TreeNodeCollection pNodes) { foreach (System.Xml.XmlElement element in nodes) { TreeNode node = new TreeNode(); node.Text = element.GetAttribute("Value"); pNodes.Add(node); LoadNode(element.ChildNodes, node.Nodes); } } //---保存节点 private void SaveTreeNode() { System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument(); xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "yes"); System.Xml.XmlElement rootNode = xmlDoc.CreateElement("root"); xmlDoc.AppendChild(rootNode); this.SaveTreeNode(xmlDoc, rootNode, this.treeView1.Nodes); string filename = AppDomain.CurrentDomain.BaseDirectory + "111.xml"; xmlDoc.Save(filename); } private void SaveTreeNode(System.Xml.XmlDocument xmlDoc, System.Xml.XmlElement rootNode, TreeNodeCollection nodes) { foreach (TreeNode node in nodes) { System.Xml.XmlElement pNode = xmlDoc.CreateElement("ChildNode"); pNode.SetAttribute("Value", node.Text); rootNode.AppendChild(pNode); SaveTreeNode(xmlDoc, pNode, node.Nodes); } }

110,549

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧