xml(可扩展标记语言)

XML 数据存储

1、XML 语法特点

1 严格区分大小写
2 有且只有一个根节点
3 有开始和结束标签
4 属性必须使用'"'
5 没有预定义标签,与html不一样
6 文档声明: <?xml verslon="1.0" encoding="utf-8"?>    //verslon="1.0" 版本
7 注释:<!-->
8 CDATA:即原意文本-<![CDATA[...]]>
9 注意编码问题,文本文件实际编码要与文档声明中编码一致

2、读取xml文件

 1  static void Main(string[] args)
 2         {
 3             //加载该文件
 4             XDocument xdoc = XDocument.Load("1.xml");
 5             //获取xml文件的根元素
 6             XElement root = xdoc.Root;
 7             Console.WriteLine(root);//获取所有的元素
 8             Console.WriteLine(root.Name);//获取根元素的名字
 9            //获取根元素中所有的子元素
10             IEnumerable<XElement> eles = root.Elements();//获得所有的子元素集合         
11             foreach (XElement item in eles)  //既然是集合就可以foreach遍历
12             {
13                 Console.WriteLine(item.Name+"----"+item.Attribute("ID").Value);//Attribute打印属性里面的属性值
14                 //获得子元素里面的子元素
15                 foreach (XElement stu in item.Elements())
16                 {
17                     Console.WriteLine(stu.Name + "----" + stu.Attribute("ID").Value);
18                     //获取元素的值
19                     Console.WriteLine(stu.Element("name").Value);
20                     Console.WriteLine(stu.Element("gender").Value);
21                     Console.WriteLine(stu.Element("age").Value);
22                 }
23             }
25             Console.ReadKey();
27         }

2、xml文件写入

 1   static void Main(string[] args)
 2         {
 3             XDocument xdoc = new XDocument();
 4             XElement root = new XElement("person");//根元素
 5             XElement stu = new XElement("student");//子元素
 6             stu.SetAttributeValue("id", "001");//设置student元素的属性和值
 7             //设置student元素下的元素和值
 8             stu.SetElementValue("name", "小明");
 9             stu.SetElementValue("gender", "");
10             stu.SetElementValue("age", "40");
11             root.Add(stu);//把子元素添加到根元素
12             xdoc.Add(root);//把元素添加到文档中
13             //保存
14             xdoc.Save("1.xml");
15             Console.WriteLine("可以的,大兄弟");
16             Console.ReadKey();
17         }
 1 using System.Collections.Generic;
 2 using System.Xml.Linq;
 3 
 4 namespace ConsoleApplication2
 5 {
 6     class Program
 7     {
 8         static void Main(string[] args)
 9         {
10             //这是一个集合,里面有三个人
11             List<Person> list = new List<Person>();
12             list.Add(new Person() { Age = 18, Gender = "", Name = "小明", ID = 001 });
13             list.Add(new Person() { Age = 20, Gender = "", Name = "小华", ID = 002 });
14             list.Add(new Person() { Age = 16, Gender = "", Name = "小红", ID = 003 });
15 
16             XDocument xdoc = new XDocument();
17             //创建根源上
18             XElement root = new XElement("Person");
19 
20             for (int i = 0; i < list.Count; i++)
21             {
22                 XElement student = new XElement("student");
23                 student.SetAttributeValue("id", list[i].ID.ToString());
24                 student.SetElementValue("name", list[i].Name);
25                 student.SetElementValue("age", list[i].Age.ToString());
26                 student.SetElementValue("gender", list[i].Gender);
27                 root.Add(student);//把节点添加到根节点中
28             }
29 
30             xdoc.Add(root);//把根源是添加到文件中
31             xdoc.Save("1.xml");//保存
32 
33         }
34     }
35     class Person
36     {
37         public int ID
38         {
39             get;
40             set;
41         }
42         public string Name
43         {
44             get;
45             set;
46         }
47         public int Age
48         {
49             get;
50             set;
51         }
52         public string Gender
53         {
54             get;
55             set;
56         }
57 
58     }
59 }

 3、遍历节点

 1 using System.Linq;
 2 using System.Windows.Forms;
 3 using System.Xml.Linq;
 4 
 5 namespace WindowsFormsApplication1
 6 {
 7     public partial class Form1 : Form
 8     {
 9         public Form1()
10         {
11             InitializeComponent();
12         }
13 
14         private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
15         {
16             //读取xml文件
17             XDocument xdoc = XDocument.Load("1.xml");
18             XElement root = xdoc.Root;//获取根元素
19             TreeNode tn = treeView1.Nodes.Add(root.Name.ToString());//根元素的名字显示到控件的根节点上
20             loadElement(root, tn);
21         }
22 
23         private void loadElement(XElement root, TreeNode tn)
24         {
25             //遍历根元素下所有的子元素
26             foreach (XElement item in root.Elements())
27             {
28                 //判断当前的元素下是否还有元素
29                 if (item.Elements().Count() > 0)//还有就接着遍历
30                 {
31                     TreeNode tn1 = tn.Nodes.Add(item.Name.ToString());
32                     loadElement(root, tn1);
33                 }
34                 else//没有就显示
35                 {
36                     tn.Nodes.Add(item.Value);
37                 }
38             }
39         }
40     }
41 }

xml登录案例

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Windows.Forms;
 4 using System.Xml.Linq;
 5 
 6 namespace WindowsFormsApplication2
 7 {
 8     public partial class Form1 : Form
 9     {
10         public Form1()
11         {
12             InitializeComponent();
13         }
14         List<User> list = new List<User>();
15         private void Form1_Load(object sender, EventArgs e)
16         {
17             //读取xml文件,获取所以的用户和密码
18 
19             XDocument xdoc = XDocument.Load("User.xml");
20             //获取根元素
21             XElement root = xdoc.Root;
22             foreach (XElement item in root.Elements())
23             {
24                 list.Add(new User()
25                 {
26                     ID = item.Attribute("id").Value,
27                     Name = item.Attribute("name").Value,
28                     Password = item.Attribute("Password").Value,
29                 });
30             }
31             //存储在集合中
32         }
33 
34         private void button1_Click(object sender, EventArgs e)
35         {
36             //登录的时候 遍历集合 判断帐号和密码 在集合中 是否存在并相同,则登录成功,否则登录失败
37             string name = textBox1.Text;
38             string pwd = textBox2.Text;
39             for (int i = 0; i < list.Count; i++)
40             {
41                 if(list[i].Name==name && list[i].Password == pwd )
42                 {
43                     MessageBox.Show("登录成功");
44                     return;
45                 }             
46             }
47             MessageBox.Show("登录失败");
48         }
49     }
50     class User
51     {
52         public string ID
53         {
54             get;
55             set;
56         }
57         public string Name
58         {
59             get;
60             set;
61         }
62         public string Password
63         {
64             get;
65             set;
66         }
67        
68     }
69 }
原文地址:https://www.cnblogs.com/lilb/p/5466439.html