XDocument
XDocument学习(Winform)
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 using System.Xml.Linq; 10 11 namespace _01XDocument 12 { 13 public partial class Form1 : Form 14 { 15 public Form1() 16 { 17 InitializeComponent(); 18 } 19 //2017-12-14 再次写一遍 20 Dictionary<string, Person> dict = new Dictionary<string, Person>(); 21 22 private void Form1_Load(object sender, EventArgs e) 23 { 24 XDocument xDoc = XDocument.Load("ListPerson.xml"); 25 XElement root = xDoc.Root; 26 27 foreach (var item in root.Elements("Person")) 28 { 29 Person model = new Person(); 30 model.Id = item.Attribute("Id").Value; 31 model.Name = item.Element("Name").Value; 32 model.Age = int.Parse(item.Element("Age").Value); 33 model.Email = item.Element("Email").Value; 34 35 //加载数据 36 dict.Add(model.Id,model); 37 listBox.Items.Add(model); 38 } 39 } 40 41 private void btnAdd_Click(object sender, EventArgs e) 42 { 43 Person model = new Person(); 44 model.Id = txtId.Text; 45 model.Name = txtName.Text; 46 model.Age = int.Parse(txtAge.Text); 47 model.Email = txtEmail.Text; 48 49 if (btnAdd.Text=="修改") 50 { 51 dict[model.Id] = model; 52 listBox.Items[listBox.SelectedIndex] = model; 53 } 54 else 55 { 56 //增加 57 dict.Add(model.Id,model); 58 listBox.Items.Add(model); 59 } 60 //清除文本框的值 61 ClearTextBox(); 62 btnAdd.Text = "添加"; 63 txtId.Enabled = true; 64 } 65 private void btnExit_Click(object sender, EventArgs e) 66 { 67 this.Close(); 68 } 69 70 /// <summary> 71 /// 清空 72 /// </summary> 73 private void ClearTextBox() 74 { 75 foreach (var item in Controls) 76 { 77 if (item is TextBox) 78 { 79 ((TextBox)item).Text = string.Empty; 80 } 81 } 82 } 83 84 private void Form1_FormClosing(object sender, FormClosingEventArgs e) 85 { 86 //关闭时候,将Dictionary中数据保存在ListPerson.xml 87 XDocument xDoc = new XDocument(); 88 XElement root = new XElement("ListPerson"); 89 xDoc.Add(root); 90 91 foreach (KeyValuePair<string,Person> item in dict) 92 { 93 XElement person = new XElement("Person"); 94 person.SetAttributeValue("Id",item.Value.Id); 95 person.SetElementValue("Name",item.Value.Name); 96 person.SetElementValue("Age",item.Value.Age); 97 person.SetElementValue("Email",item.Value.Email); 98 99 root.Add(person); 100 } 101 xDoc.Save("ListPerson.xml"); 102 } 103 private void listBox_SelectedIndexChanged(object sender, EventArgs e) 104 { 105 Person model = listBox.SelectedItem as Person; 106 txtId.Text = model.Id; 107 txtName.Text = model.Name; 108 txtAge.Text = model.Age.ToString(); 109 txtEmail.Text = model.Email; 110 111 btnAdd.Text = "修改"; 112 txtId.Enabled = false; 113 } 114 115 /// <summary> 116 /// 构造Person类 117 /// </summary> 118 public class Person 119 { 120 public Person() 121 { 122 123 } 124 public Person(string id, string name, int age, string email) 125 { 126 this.Id = id; 127 this.Name = name; 128 this.Age = age; 129 this.Email = email; 130 } 131 public string Id { get; set; } 132 public string Name { get; set; } 133 public int Age { get; set; } 134 public string Email { get; set; } 135 public override string ToString() 136 { 137 //return base.ToString(); 138 return this.Name; 139 } 140 } 141 142 } 143 }