c# XML增删改查
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Xml; namespace _04对XML实现增删改查 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } List<User> listUsers = new List<User>(); private void Form1_Load(object sender, EventArgs e) { LoadData(); } //加载数据 private void LoadData() { // listUsers = new List<User>(); //设置DataGridView的样式 //取消显示第一列 dataGridView1.RowHeadersVisible = false; //将单元格选中变成整行选中 dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect; XmlDocument doc = new XmlDocument(); doc.Load("haodongxi.xml"); //获得根节点 XmlElement users = doc.DocumentElement; //获得根节点下面所有的子节点 XmlNodeList xnl = users.ChildNodes; listUsers.Clear(); foreach (XmlNode item in xnl) { User user = new User(); user.ID = int.Parse(item.Attributes["id"].Value); user.Name = item["name"].InnerText; user.Age = int.Parse(item["age"].InnerText); user.Gender = char.Parse(item["gender"].InnerText); user.Password = item["password"].InnerText; listUsers.Add(user); } //把集合作为数据源赋值给DatagridView dataGridView1.DataSource = null; //如果要dgv数据放生改变,则必须要给它的DataSource重新复制 dataGridView1.DataSource = listUsers; } private void btnZhuCe_Click(object sender, EventArgs e) { XmlDocument doc = new XmlDocument(); doc.Load("haodongxi.xml"); XmlElement users = doc.DocumentElement; XmlElement user = doc.CreateElement("user"); user.SetAttribute("id", txtID.Text.Trim()); users.AppendChild(user); XmlElement name = doc.CreateElement("name"); name.InnerText = txtName.Text; user.AppendChild(name); XmlElement age = doc.CreateElement("age"); age.InnerText = txtAge.Text; user.AppendChild(age); XmlElement gender = doc.CreateElement("gender"); gender.InnerText = rdoMan.Checked ? "男" : "女"; user.AppendChild(gender); XmlElement password = doc.CreateElement("password"); password.InnerText = txtPwd.Text; user.AppendChild(password); //切记 保存 doc.Save("haodongxi.xml"); //重新加载数据 LoadData(); MessageBox.Show("注册成功"); } private void button1_Click(object sender, EventArgs e) { string id = txtUpdateID.Text; XmlDocument doc = new XmlDocument(); doc.Load("haodongxi.xml"); XmlElement users = doc.DocumentElement; //xPath Path XMLPath XmlNode xn = users.SelectSingleNode("/Users/user[@id='" + id + "']"); // /"Users/user[@id='" + "']" //"/Users/user[@id='']" xn["name"].InnerText = txtUpdateName.Text.Trim(); xn["age"].InnerText = txtUpdateAge.Text.Trim(); xn["gender"].InnerText = rdoUpdateMan.Checked ? "男" : "女"; xn["password"].InnerText = txtUpdatePwd.Text; doc.Save("haodongxi.xml"); LoadData(); MessageBox.Show("修改成功"); } private void 删除ToolStripMenuItem_Click(object sender, EventArgs e) { //删除指定数据 //获得选中行的ID string id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString(); XmlDocument doc = new XmlDocument(); doc.Load("haodongxi.xml"); //获得根节点 XmlElement users = doc.DocumentElement; //通过根节点 使用XPath路径找到id=...的节点; XmlNode xn = users.SelectSingleNode("/Users/user[@id='" + id + "']"); //开始删除 users.RemoveChild(xn); doc.Save("haodongxi.xml"); MessageBox.Show("删除成功"); LoadData(); } private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { txtUpdateID.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString(); txtUpdateName.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString(); txtUpdateAge.Text = dataGridView1.SelectedRows[0].Cells[2].Value.ToString(); string gender = dataGridView1.SelectedRows[0].Cells[3].Value.ToString(); if (gender == "男") { rdoUpdateMan.Checked = true; } else { rdoUpdateWoman.Checked = true; } txtUpdatePwd.Text = dataGridView1.SelectedRows[0].Cells[4].Value.ToString(); } } }