-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm3.cs
177 lines (155 loc) · 4.91 KB
/
Form3.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
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;
using System.IO;
using ButtonTest;
namespace SvDemo
{
public partial class Form3 : Form
{
private string xmlFileName;
public Form3()
{
InitializeComponent();
}
#region 添加/编辑节点方法
private StreamWriter sr;
public void exportToXml(TreeView tv, string filename)
{
sr = new StreamWriter(filename, false, System.Text.Encoding.UTF8);
//Write the header
sr.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
//Write our root node
sr.WriteLine("<" + treeView1.Nodes[0].Text + " number=\""+Form1.number_pres+"\""+" >");
foreach (TreeNode node in tv.Nodes)
{
saveNode(node.Nodes);
}
//Close the root node
sr.WriteLine("</" + treeView1.Nodes[0].Text + ">");
sr.Close();
}
//保存xml
private void saveNode(TreeNodeCollection tnc)
{
foreach (TreeNode node in tnc)
{
if (node.Nodes.Count > 0)
{
sr.WriteLine("<" + node.Text + ">");
saveNode(node.Nodes);
sr.WriteLine("</" + node.Text + ">");
}
else
{
sr.WriteLine(node.Text);
}
}
}
#endregion
private void btnModify_Click(object sender, EventArgs e)
{
if (txtModify.Text != "")
{
string ss = treeView1.SelectedNode.Text;
treeView1.SelectedNode.Text = txtModify.Text;
txtModify.Text = "";
exportToXml(treeView1, xmlFileName);
}
else
{
MessageBox.Show("请输入被编辑节点的名称!");
}
}
public TreeView getView()
{
return this.treeView1;
}
public void setXMLFileName(string str)
{
xmlFileName = str;
}
//添加节点
public void addTreeNode(XmlNode xmlNode, TreeNode treeNode)
{
XmlNode xNode;
TreeNode tNode;
XmlNodeList xNodeList;
if (xmlNode.HasChildNodes)
{
xNodeList = xmlNode.ChildNodes;
for (int x = 0; x <= xNodeList.Count - 1; x++)
{
xNode = xmlNode.ChildNodes[x];
treeNode.Nodes.Add(new TreeNode(xNode.Name));
tNode = treeNode.Nodes[x];
addTreeNode(xNode, tNode);
}
}
else
{
treeNode.Text = xmlNode.OuterXml.Trim();
}
}
private void btnAdd_Click(object sender, EventArgs e)
{
if (txtAdd.Text != "")
{
TreeView treeView = new TreeView();
treeView1.SelectedNode.Nodes.Add(txtAdd.Text.Trim());
txtAdd.Text = "";
exportToXml(treeView1, xmlFileName);
}
else
{
MessageBox.Show("尚未输入节点的名称!");
}
}
private void btnDelete_Click(object sender, EventArgs e)
{
treeView1.Nodes.Remove(treeView1.SelectedNode);
delexportToXml(treeView1, xmlFileName);
}
# region 删除节点方法
public void delexportToXml(TreeView tv, string filename)
{
sr = new StreamWriter(filename, false, System.Text.Encoding.UTF8);
sr.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
sr.WriteLine("<" + treeView1.Nodes[0].Text + ">");
foreach (TreeNode node in tv.Nodes)
{
delNode(node.Nodes);
}
sr.WriteLine("</" + treeView1.Nodes[0].Text + ">");
sr.Close();
}
//保存xml
private void delNode(TreeNodeCollection tnc)
{
XmlDocument xDoc = new XmlDocument();
foreach (TreeNode node in tnc)
{
if (node.Nodes.Count > 0)
{
sr.WriteLine("<" + node.Text + ">");
if (treeView1.Nodes[0].Name == node.Text) //遍历出的节点为当前所选节点则删除
{
xDoc.RemoveAll();
}
saveNode(node.Nodes);
sr.WriteLine("</" + node.Text + ">");
}
else
{
sr.WriteLine(node.Text);
}
}
}
#endregion
}
}