資源簡介
winfrom中TreeView控件XML與樹之間相互轉(zhuǎn)換
代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.Text;
using?System.Windows.Forms;
using?System.xml;
namespace?Menu
{
????public?partial?class?Form1?:?Form
????{
????????public?Form1()
????????{
????????????InitializeComponent();
????????}
????????#region?Edit?TreeView
????????private?void?btnAddRoot_Click(object?sender?EventArgs?e)
????????{
????????????TreeNode?node?=?CreateNewNode();
????????????this.treeView1.Nodes.Add(node);
????????}
????????private?TreeNode?CreateNewNode()
????????{
????????????TreeNode?node?=?new?TreeNode(this.txttitle.Text.Trim());
????????????node.Tag?=?this.txtContent.Text.Clone();
????????????return?node;
????????}
????????private?void?btnAddChild_Click(object?sender?EventArgs?e)
????????{
????????????if?(this.treeView1.SelectedNode?==?null)
????????????{
????????????????return;
????????????}
????????????TreeNode?node?=?CreateNewNode();
????????????this.treeView1.SelectedNode.Nodes.Add(node);
????????????this.treeView1.SelectedNode.Expand();
????????}
????????private?void?btnDelSel_Click(object?sender?EventArgs?e)
????????{
????????????if?(this.treeView1.SelectedNode?==?null)
????????????{
????????????????return;
????????????}
????????????this.treeView1.Nodes.Remove(this.treeView1.SelectedNode);
????????}
????????#endregion
????????#region?TreeView?2?xml
????????private?void?btnSave_Click(object?sender?EventArgs?e)
????????{
????????????//將TreeView保存到xml文件中
????????????if?(this.dlgSave.ShowDialog()?==?DialogResult.OK)
????????????{
????????????????xmlDocument?doc?=?new?xmlDocument();
????????????????doc.Loadxml(““);
????????????????xmlNode?root?=?doc.DocumentElement;
????????????????doc.InsertBefore(doc.CreatexmlDeclaration(“1.0“?“utf-8“?“yes“)?root);
????????????????TreeNode2xml(this.treeView1.Nodes?root);
????????????????doc.Save(dlgSave.FileName);
????????????}
????????}
????????private?void?TreeNode2xml(TreeNodeCollection?treeNodes?xmlNode?xmlNode)
????????{
????????????xmlDocument?doc?=?xmlNode.OwnerDocument;
????????????foreach?(TreeNode?treeNode?in?treeNodes)
????????????{
????????????????xmlNode?element?=?doc.CreateNode(“element“?“Item“?““);
????????????????xmlAttribute?attr?=?doc.CreateAttribute(“title“);
????????????????attr.Value?=?treeNode.Text;
????????????????element.Attributes.Append(attr);
????????????????element.AppendChild(doc.CreateCDataSection(treeNode.Tag.ToString()));
????????????????xmlNode.AppendChild(element);
????????????????if?(treeNode.Nodes.Count?>?0)
????????????????{
????????????????????TreeNode2xml(treeNode.Nodes?element);
????????????????}
????????????}
????????}
????????#endregion
????????#region?xml?2?TreeView
????????private?void?btnLoad_Click(object?sender?EventArgs?e)
????????{
????????????//從xml中讀取數(shù)據(jù)到TreeView
????????????if?(this.dlgOpen.ShowDialog()?==?DialogResult.OK
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件??????24576??2006-12-01?11:18??Menu\Menu\bin\Debug\Menu.exe
?????文件????????178??2006-12-01?08:53??Menu\Menu\bin\Debug\Menu.xm
?????目錄??????????0??2006-12-01?08:58??Menu\Menu\bin\Debug
?????目錄??????????0??2006-12-01?08:52??Menu\Menu\bin\Release
?????目錄??????????0??2006-12-01?08:52??Menu\Menu\bin
?????文件???????5544??2006-12-01?11:09??Menu\Menu\Form1.cs
?????文件???????9830??2006-12-01?10:41??Menu\Menu\Form1.Designer.cs
?????文件???????6411??2006-12-01?10:41??Menu\Menu\Form1.resx
?????文件???????3365??2006-12-01?08:53??Menu\Menu\Menu.csproj
?????文件????????178??2006-12-01?08:53??Menu\Menu\Menu.xm
?????文件????????471??2006-12-01?08:39??Menu\Menu\Program.cs
?????文件???????1311??2006-12-01?11:05??Menu\Menu\Properties\AssemblyInfo.cs
?????文件???????2835??2006-12-01?08:39??Menu\Menu\Properties\Resources.Designer.cs
?????文件???????5612??2006-12-01?08:39??Menu\Menu\Properties\Resources.resx
?????文件???????1087??2006-12-01?08:39??Menu\Menu\Properties\Settings.Designer.cs
?????文件????????249??2006-12-01?08:39??Menu\Menu\Properties\Settings.settings
?????目錄??????????0??2006-12-01?11:05??Menu\Menu\Properties
?????目錄??????????0??2006-12-01?11:09??Menu\Menu
?????文件????????901??2006-12-01?08:52??Menu\Menu.sln
????..A..H.?????13312??2006-12-01?11:05??Menu\Menu.suo
?????目錄??????????0??2006-12-01?08:52??Menu
-----------?---------??----------?-----??----
????????????????75860????????????????????21
評論
共有 條評論