码迷,mamicode.com
首页 > 其他好文 > 详细

2021-3-13 xml的增删改查

时间:2021-03-15 10:59:05      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:edit   pen   exce   lse   help   use   index   har   rgb   

  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>();
            }
        }

以上是可以放在xmlHelp直接调用的方法

  public class People
    {
        public string Name;
        public string Sex;
        public string Age;
        public string Id;
        public People() { }
        public People(string Name, string Sex, string Age, string Id)
        {
            this.Name = Name;
            this.Sex = Sex;
            this.Age = Age;
            this.Id = Id;
        }

    }

 

2021-3-13 xml的增删改查

标签:edit   pen   exce   lse   help   use   index   har   rgb   

原文地址:https://www.cnblogs.com/WH5212/p/14527455.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!