标签: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; } }
标签:edit pen exce lse help use index har rgb
原文地址:https://www.cnblogs.com/WH5212/p/14527455.html