2021-3-9 xml序列化和反序列化

class XmlHelp
    {
        #region 调用
        /// <summary>
        /// xml添加
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="people"></param>
        public void XmlAdd(string filename, List<People> pList)
        {
            try
            {
                List<People> peoples = XmlDeSerializer(filename);
                foreach (var item in pList)
                {
                    People people = new People();
                    people.Name = string.IsNullOrEmpty(item.Name) ? "" : item.Name;
                    people.Id = string.IsNullOrEmpty(item.Id) ? "" : item.Id;
                    people.Age = string.IsNullOrEmpty(item.Age) ? "" : item.Age;
                    people.Sex = string.IsNullOrEmpty(item.Sex) ? "" : item.Sex;
                    peoples.Add(people);
                }
                XmlSerializer x = new XmlSerializer(typeof(List<People>));
                TextWriter writer = new StreamWriter(filename);
                x.Serialize(writer, peoples);
                writer.Dispose();
            }
            catch (Exception ex)
            {

            }
        }
        /// <summary>
        /// xml编辑
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="people"></param>
        public void XmlEdit(string filename, List<People> pList, int index)
        {
            try
            {
                XmlDelete("User", index);
                List<People> peoples = XmlDeSerializer(filename);
                foreach (var item in pList)
                {
                    People people = new People();
                    people.Name = string.IsNullOrEmpty(item.Name) ? "" : item.Name;
                    people.Id = string.IsNullOrEmpty(item.Id) ? "" : item.Id;
                    people.Age = string.IsNullOrEmpty(item.Age) ? "" : item.Age;
                    people.Sex = string.IsNullOrEmpty(item.Sex) ? "" : item.Sex;
                    peoples.Insert(index, people);
                }
                XmlSerializer x = new XmlSerializer(typeof(List<People>));
                TextWriter writer = new StreamWriter(filename);
                x.Serialize(writer, peoples);
                writer.Dispose();
            }
            catch (Exception ex)
            {

            }
        }
        /// <summary>
        /// 删除列表
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="pList"></param>
        public void XmlDelete(string filename, int index = 0)
        {
            try
            {
                List<People> peoples = XmlDeSerializer(filename);
                peoples.RemoveAt(index);
                XmlSerializer x = new XmlSerializer(typeof(List<People>));
                TextWriter writer = new StreamWriter(filename);
                x.Serialize(writer, peoples);
                writer.Dispose();
            }
            catch (Exception ex)
            {

            }
        }
        /// <summary>
        /// 显示列表
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="pList"></param>
        public List<People> XmlDeSerializer(string filename)
        {
            try
            {
                var mySerializer = new XmlSerializer(typeof(List<People>));
                var myFileStream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                if (myFileStream.Length > 0)
                {
                    var myObject = (List<People>)mySerializer.Deserialize(myFileStream);
                    return myObject;
                }

                myFileStream.Dispose();
                return new List<People>();
            }
            catch (Exception ex)
            {
                return new List<People>();
            }
        }#endregion
    }

可以创建一个类直接复制上方的代码,然后将people换成一个新类,并将people中的成员替换

 XmlHelp xmlHelp = new XmlHelp();
      private void InfoListView_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (InfoListView.SelectedItems.Count > 0)
            {
                this.NameTxt.Text = InfoListView.SelectedItems[0].SubItems[0].Text;
                this.SexTxt.Text = InfoListView.SelectedItems[0].SubItems[1].Text;
                this.AgeTxt.Text = InfoListView.SelectedItems[0].SubItems[2].Text;
                this.IdTxt.Text = InfoListView.SelectedItems[0].SubItems[3].Text;
            }
        }

        private void DeleteBtn_Click(object sender, EventArgs e)
        {
            try
            {
                if (InfoListView.SelectedItems.Count > 0)
                {
                    var index = InfoListView.SelectedItems[0].Index;
                    xmlHelp.XmlDelete("User", index);
                    ListLoad();
                    TextClear();
                    MessageBox.Show("删除成功");
                }
                else
                {
                    MessageBox.Show("请选择想要删除的数据");
                }
            }
            catch
            {
                MessageBox.Show("出现错误");
            }
        }

        private void UpdateBtn_Click(object sender, EventArgs e)
        {
            try
            {
                if (InfoListView.SelectedItems.Count > 0)
                {
                    var index = InfoListView.SelectedItems[0].Index;
                    People people = new People();
                    people.Name = NameTxt.Text;
                    people.Id = IdTxt.Text;
                    people.Age = AgeTxt.Text;
                    people.Sex = SexTxt.Text;
                    var peoples = new List<People>();
                    peoples.Add(people);
                    xmlHelp.XmlEdit("User", peoples, index);
                    ListLoad();
                    TextClear();
                    MessageBox.Show("更新成功");
                }
                else
                {
                    MessageBox.Show("请选择想要修改的数据");
                }
            }
            catch
            {
                MessageBox.Show("出现错误");
            }

        }

        private void AddBtn_Click(object sender, EventArgs e)
        {
             try
            {
                People people = new People();
                people.Name = NameTxt.Text;
                people.Id = IdTxt.Text;
                people.Age = AgeTxt.Text;
                people.Sex = SexTxt.Text;
                var peoples = new List<People>();
                peoples.Add(people);
                xmlHelp.XmlAdd("User", peoples);
                ListLoad();
                TextClear();
                MessageBox.Show("添加成功");
            }
            catch
            {
                MessageBox.Show("出现错误");
            }
        }

        private void XMLFrm_Load(object sender, EventArgs e)
        {
            ListLoad();
        }

        public void ListLoad()
        {
            try
            {
                this.InfoListView.Clear();
                var pList = xmlHelp.XmlDeSerializer("User");
                this.InfoListView.View = View.Details;
                this.InfoListView.Columns.Add("编号", 100, HorizontalAlignment.Left);
                this.InfoListView.Columns.Add("姓名", 100, HorizontalAlignment.Left);
                this.InfoListView.Columns.Add("性别", 100, HorizontalAlignment.Left);
                this.InfoListView.Columns.Add("年龄", 100, HorizontalAlignment.Left);

                foreach (var item in pList)
                {
                    ListViewItem lvi = new ListViewItem(item.Id);
                    lvi.SubItems.Add(item.Name);
                    lvi.SubItems.Add(item.Sex);
                    lvi.SubItems.Add(item.Age);
                    this.InfoListView.Items.Add(lvi);
                }
            }
            catch
            {
                MessageBox.Show("出现错误");
            }
        }
        void TextClear()
        {
            this.NameTxt.Text = "";
            this.SexTxt.Text = "";
            this.AgeTxt.Text = "";
            this.IdTxt.Text = "";
        }

窗体的cs中的内容,分别是删除,修改,添加,加载在表格和清除text中的内容。

上一篇:支付宝的当面付扫一扫功能


下一篇:助您提升商品详情页质量