这篇文章主要为大家详细介绍了C# winfrom实现读取修改xml的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
这篇文章主要为大家详细介绍了C# winfrom实现读取修改xml的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
这篇文章主要为大家详细介绍了C# winfrom实现读取修改xml的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文示例为大家分享了winfrom实现读取修改xml的具体代码,供大家参考,具体内容如下
在winfrom窗体中放一个文本框,2个按钮,一个panle,如下图

form.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
|
using system;using system.collections.generic;using system.componentmodel;using system.data;using system.drawing;using system.linq;using system.text;using system.windows.forms;using system.xml;namespace xmlconfiger{ public partial class form1 : form { public form1() { initializecomponent(); } public string path; xmlconfig xmlconfig; //读取xml内容 private void button1_click(object sender, eventargs e) { openfiledialog filename = new openfiledialog();//定义一个文件打开控件 filename.filter = "所有xml文件|*.xml";//设置控件打开的文件类型 filename.filterindex = 2;//设置控件打开文件类型的显示顺序 filename.restoredirectory = true;//设置对话框是否记忆之前打开的目录 if (filename.showdialog() == dialogresult.ok) { path = filename.filename.tostring();//获得用户选择的完整路径 name = path.substring(path.lastindexof("\\") + 1);//获取用户选择的不带路径的文件名 xmlconfig = new xmlconfig(path); int count = xmlconfig.getcount(); int ysplit = 30; int x1 = 3; for (int i = 0; i < count; i++) { label lb = new label(); lb.text = xmlconfig.getname(i).tostring(); lb.tag = ""; lb.size = new system.drawing.size(60, 23); lb.autosize = false; textbox tb = new textbox(); tb.text = xmlconfig.getxmlnode(i).tostring(); tb.tag = i; lb.location = new point(x1, i * ysplit); tb.location = new point(x1 + lb.size.width + 10, i * ysplit); panel1.controls.add(lb); panel1.controls.add(tb); } } } //修改xml内容 private void button2_click(object sender, eventargs e) { for (int i = 0; i < this.panel1.controls.count; i++) { if (this.panel1.controls[i].tag != null && this.panel1.controls[i].tag.tostring() != "") { textbox textbox1 = (textbox)(this.panel1.controls[i]); xmlconfig.savaxmlconfig(convert.toint32(textbox1.tag), textbox1.text); } } xmlconfig.savaconfig(); } }} |
xmlconfig.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
|
using system;using system.collections.generic;using system.linq;using system.text;using system.xml;using system.io;using system.data;using system.windows.forms;namespace xmlconfiger{ public class xmlconfig { public int count = 0; public string path=""; private list<string> strlist = new list<string>(); private list<string> listname = new list<string>(); //构造函数获得所有信息 public xmlconfig(string path) { xmldocument xmldoc = new xmldocument(); xmldoc.load(path);//读取指定的xml文档 path = path; xmlnode roomlist = xmldoc.selectsinglenode("rss"); xmlnodelist list = roomlist.childnodes; foreach (xmlnode item in list) { listname.add(item.attributes["name"].value); strlist.add(item.innertext); count = listname.count; } } //获取所有节点的个数 public int getcount() { return count; } //通过tag值获取当前返回的name public string getname(int tag) { return listname[tag]; } //通过tag值获取当前返回的value public string getxmlnode(int tag) { return strlist[tag]; } //修改xml中所有的内容 public void savaconfig() { xmldocument xmldoc = new xmldocument(); xmldoc.load(path); xmlnodelist nodelist=xmldoc.selectsinglenode("rss").childnodes;//获取节点的所有子节点 for (int i = 0; i < nodelist.count; i++)//遍历所有子节点 { xmlelement xe = (xmlelement)nodelist[i]; xmlnode childxml = nodelist[i]; for (int j = 0; j < strlist.count; j++) { if (listname[j] == childxml.attributes["name"].value) { xe.setattribute("name", listname[i]); xe.innertext = strlist[i]; break; } } } xmldoc.save(path);//保存。 } //修改xml中某一个节点 public void savaxmlconfig(int tag, string name) { strlist[tag] = name; } }} |
xml文件:
|
1
2
3
4
5
6
|
<?xml version="1.0" encoding="utf-8"?><rss version="2.0"> <student name="姓名">宁泽涛</student> <age name="年龄">22</age> <hobby name="爱好">游泳</hobby></rss> |
以上就是本文的全部内容,希望对大家的学习有所帮助。
发表评论