WinForm ListView对XML增删改查


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 XML增删改查
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            DisPlayData();

        }
        /// <summary>
        /// xml显示到ListView
        /// </summary>
        private void DisPlayData()
        {
            listView1.Items.Clear();
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load("1.xml");
                XmlNodeList nodeList = doc.SelectNodes("/person/list");//xPath相关手册,找节点person的list

                foreach (XmlNode node in nodeList)
                {
                    ListViewItem viewItem = new ListViewItem(node.Attributes["id"].Value);
                    viewItem.SubItems.Add(node.SelectSingleNode("loginID").InnerText);
                    viewItem.SubItems.Add(node.SelectSingleNode("pwd").InnerText);

                    listView1.Items.Add(viewItem);
                }
            }
            catch (Exception ex)
            {

            }
        }
        /// <summary>
        /// 添加XML元素
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnADD_Click(object sender, EventArgs e)
        {
            string id = txtID.Text;
            string loginID = txtLoginID.Text;
            string pwd = txtpwd.Text;
            XmlDocument doc = new XmlDocument();
            doc.Load("1.xml");
            if (doc.SelectNodes("/person/list[@id='" + id + "']").Count > 0)
            {
                MessageBox.Show("ID重复");
                return;
            }
            else
            {
                XmlElement root = doc.DocumentElement;
                XmlElement newElement = doc.CreateElement("list");
                newElement.SetAttribute("id", id);//添加属性
                root.AppendChild(newElement);
                XmlElement xmlLogin = doc.CreateElement("loginID");
                xmlLogin.InnerText = loginID;
                newElement.AppendChild(xmlLogin);
                XmlElement xmlpwd = doc.CreateElement("pwd");
                xmlpwd.InnerText = pwd;
                newElement.AppendChild(xmlpwd);
                root.AppendChild(newElement);
                doc.Save("1.xml");
                DisPlayData();
            }

        }
    //选中获得listView的值
        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count <= 0) { return; }
            ListViewItem item = listView1.SelectedItems[0];
            txtEditID.Text = item.SubItems[0].Text;
            txtEditLoginID.Text = item.SubItems[1].Text;
            txtEditpwd.Text = item.SubItems[2].Text;
        }
    //编辑XML
        private void btnEdit_Click(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("1.xml");
            XmlNodeList xmlNodeList = doc.SelectNodes("/person/list[@id='" + txtEditID.Text + "']");
            xmlNodeList[0].ChildNodes[0].InnerText = txtEditLoginID.Text;
            xmlNodeList[0].ChildNodes[1].InnerText= txtEditpwd.Text;
            doc.Save("1.xml");
            DisPlayData();
        }
    //删除XML选中的节点
        private void btnDelete_Click(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("1.xml");
            XmlElement root = doc.DocumentElement;
            XmlNodeList xmlNodeList = doc.SelectNodes("/person/list[@id='" + txtEditID.Text + "']");
            foreach (XmlNode xmlNode in xmlNodeList)
            {
                xmlNode.ParentNode.RemoveChild(xmlNode);
            }
            doc.Save("1.xml");
            DisPlayData();
        }
    }
}
以下是   1.XML文件内容  注意一定要把1.XML文件放到Debug根目录下,不然找不到文件会出异常
<?xml version="1.0" encoding="utf-8"?>
<person>
  <list id="1">
    <loginID>罗</loginID>
    <pwd>分明</pwd>
  </list>
  <list id="2">
    <loginID>luofenming</loginID>
    <pwd>luofenming</pwd>
  </list>
  <list id="3">
    <loginID>测试</loginID>
    <pwd>密码</pwd>
  </list>
</person>


评论