首页 > Windows开发 > 详细

1、c#对XML文件的读取

时间:2015-12-29 14:21:29      阅读:224      评论:0      收藏:0      [点我收藏+]

1、XML文件

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <PersonF xmlns="" Name="(test)work hard work smart!">
 3   <person Name="Person1">
 4     <ID>1</ID>
 5     <Name>XiaoA</Name>
 6     <Age>59</Age>
 7   </person>
 8   <person Name="Person2">
 9     <ID>2</ID>
10     <Name>XiaoB</Name>
11     <Age>29</Age>
12   </person>
13   <person Name="Person3">
14     <ID>3</ID>
15     <Name>XiaoC</Name>
16     <Age>103</Age>
17   </person>
18   <person Name="Person4">
19     <ID>4</ID>
20     <Name>XiaoD</Name>
21     <Age>59</Age>
22   </person>
23 </PersonF>

2、程序代码

 1        string path = @"E:\测试\XML读取\XML.xml";
 2             XmlDocument xd = new XmlDocument();
 3             try
 4             {
 5                 xd.Load(path);
 6                 switch (count)
 7                 {
 8                     case 0:
 9                         //1、读取单个节点的数据
10                         XmlNode xn1= xd.SelectSingleNode("PersonF");
11                         richTextBox1.Text = xn1.InnerText.ToString();
12                         count++;break;
13                     case 1:
14                         //2、读取多个节点的数据
15                         XmlNodeList xn2 = xd.SelectNodes("PersonF/person");
16                         foreach (XmlNode xn in xn2)
17                         {
18                             richTextBox1.Text += xn.InnerText;
19                         }
20                         count++;break;
21                     case 2:
22                         //3.1、读取具体节点的具体值 如:Name属性为Person2的Name的InnerText
23                         XmlNodeList xn3 = xd.DocumentElement.GetElementsByTagName("person");
24                         foreach (XmlNode xn in xn3)
25                         {
26                             if (xn.Attributes["Name"].InnerText == "Person2")
27                             {
28                                 richTextBox1.Text += xn.InnerText;
29                             }
30                         }
31                         count++; break;
32                     case 3:
33                         //3.2、读取ID为2所在的节点第二个子节点Name的InnerText
34                         XmlNode xn4 = xd.SelectSingleNode("PersonF/person[ID=2]");
35                         richTextBox1.Text = xn4.ChildNodes[1].InnerText;
36                         count++; break;
37                     case 4:
38                         //3.3、找到ID为2的节点的父节点Name属性
39                         XmlNodeList xn5 = xd.SelectNodes("//person//ID");//读取ID为2的节点
40                         foreach (XmlNode xn in xn5)
41                         {
42                             if (xn.InnerText == "2")
43                             {
44                                 richTextBox1.Text += xn.ParentNode.Attributes["Name"].InnerText;
45                             }
46                         }
47                         count++;break;
48                     case 5:
49                         //4、修改节点属性
50                         XmlNode xn6 = xd.SelectSingleNode("PersonF");
51                         xn6.Attributes["Name"].InnerText = "helloworld";
52                         xd.Save(path);
53                         count++; break;
54                     case 6:
55                         //5、添加自定义节点
56                         XmlTextReader xr = new XmlTextReader(path);
57                         XmlElement root = xd.DocumentElement;//根节点(最外层节点)
58 
59                         XmlElement newele = xd.CreateElement("person");//第二层节点
60                         newele.SetAttribute("Name", "Person6");
61 
62                         XmlElement ele_id = xd.CreateElement("ID");//newele子节点
63                         ele_id.InnerText = "5";
64                         XmlElement ele_name = xd.CreateElement("Name");//newele子节点
65                         ele_name.InnerText = "XiaoE";
66                         newele.AppendChild(ele_id);
67                         newele.AppendChild(ele_name);
68                         root.AppendChild(newele);
69 
70                         xr.Close();
71                         xd.Save(path);
72 
73                         count=0; break;
74                 }
75 
76             }
77             catch 
78             {
79                 Exception ex;
80             }

 源码下载:http://pan.baidu.com/s/1dDRzZ4P

1、c#对XML文件的读取

原文:http://www.cnblogs.com/wleaves/p/5085467.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!