感谢您阅读我的博客,如果您现在工作、学习累了或者疲惫了,不妨聆听一下音乐,它能够减轻你的疲劳,还能够带给您一种舒适愉悦的心情。如果您认为这篇文章还不错或者有所收获,您可以在页面 右侧和底部 扫描二维码 打赏我,您的鼓励是我继续写作、分享的最大动力!

WinForm里序列化读写XML

using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using System.Xml.Serialization;
using MessageBoxEffects.MyClass;

namespace WinFormXmlSerializeObjectDemo
{
    /// <summary>
    ///     WinForm里序列化读写XML
    ///     LDH @ 2021-3-29
    /// </summary>
    public partial class FrmMain : Form
    {
        public readonly List<Student> StuList = new List<Student>();

        public FrmMain()
        {
            InitializeComponent();
            InitStudents();
        }

        private void InitStudents()
        {
            StuList.AddRange(new[]
            {
                new Student {Id = 1, Age = 18, Name = "LDH"},
                new Student {Id = 2, Age = 28, Name = "Lily"},
                new Student {Id = 3, Age = 38, Name = "Lucy"},
                new Student {Id = 4, Age = 40, Name = "Tom"},
                new Student {Id = 5, Age = 58, Name = "Jerry"}
            });
        }


        /// <summary>
        ///     序列化对象,并写入XML文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnWriteIntoXml_Click(object sender, EventArgs e)
        {
            var serializer = new XmlSerializer(typeof(List<Student>));

            var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Student.xml");
            using (var stream = new FileStream(path, FileMode.OpenOrCreate))
            {
                serializer.Serialize(stream, StuList);
                MyHelper.ShowMessageBoxInfo($"成功写入到Xml文件!{Environment.NewLine}文件路径:{path}");
            }
        }

        /// <summary>
        ///     读取XML文件,并反序列化为对象,并绑定到ListBox控件进行展示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnReadFromXml_Click(object sender, EventArgs e)
        {
            var serializer = new XmlSerializer(typeof(List<Student>));

            var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Student.xml");
            using (var stream = new FileStream(path, FileMode.OpenOrCreate))
            {
                var stuList = serializer.Deserialize(stream);

                listStudent.DataSource = null;

                listStudent.BeginUpdate();
                listStudent.DataSource = stuList;
                listStudent.EndUpdate();
            }
        }
    }

    public class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }

        public override string ToString()
        {
            return $"{Id},{Name},{Age}岁";
        }
    }
}

posted @ 2021-03-29 15:47  Love In Winter  阅读(291)  评论(0编辑  收藏  举报
作者: LifeDecidesHappiness
出处: http://www.cnblogs.com/LifeDecidesHappiness/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,否则保留追究法律责任的权利,且在文章页面明显位置给出原文连接,如有问题,可以通过以下邮箱地址 2468881301@qq.com  联系我,非常感谢。
踏实做一个为人民服务的搬运工!
如果您认为这篇文章还不错或者有所收获,您可以通过右边的“打赏”功能,您的支持和鼓励是我继续写作、分享的最大动力!

点击关注不迷路,让我带你上高速!