Winform对XML文件的增删改查

用于测试的Model

class Ticket { public Ticket(int id, double total, DateTime day) { Id = id; Total = total; Day = day; } public int Id { get; set; } public double Total { get; set; } public DateTime Day { get; set; } }

建立一个XMLHelper类
默认配置及初始化对象
static string Path = "DataBase.xml"; static XDocument xdocument = XDocument.Load(Path);

创建文件方法
public static void CreateXmlFile() { XDocument xdoc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"), CreateXElement()); xdoc.Save(Path); }private static XElement CreateXElement() { XElement root = new XElement("Root"); return root; }

添加对象方法,将需要创建的对象作为参数传入即可
public static void Insert(Ticket Obj) { XElement InsertRoot = new XElement("ID", new XElement("UserID", Obj.Id), new XElement("Total", Obj.Total), new XElement("Day", Obj.Day)); xdocument.Element("Root").Add(InsertRoot); xdocument.Save(Path); }

修改方法,第一个参数是ID号,第二个参数是修改后的对象
public static void Change(int ID,Ticket Obj) { XElement UserUpdate = (from userInfo in xdocument.Element("Root").Elements() where Convert.ToInt32(userInfo.Element("UserID").Value) == ID select userInfo).SingleOrDefault(); if (UserUpdate != null) { UserUpdate.Element("UserID").Value = https://www.it610.com/article/Obj.Id.ToString(); UserUpdate.Element("Total").Value = https://www.it610.com/article/Obj.Total.ToString(); UserUpdate.Element("Day").Value = https://www.it610.com/article/Obj.Day.ToString(); xdocument.Save(Path); } }

删除方法,直接使用ID号进行删除操作
public static void Del(int ID) { XElement UserDelete = (from userInfo in xdocument.Element("Root").Elements() where Convert.ToInt32(userInfo.Element("UserID").Value) == ID select userInfo).SingleOrDefault(); if (UserDelete != null) { UserDelete.Remove(); xdocument.Save(Path); } }

全部遍历&按ID号查询;ID=0时为全部遍历,ID≠0时为使用ID号查询
public static List ReSearch(int ID) { List ObjList = new List(); if (ID == 0) { var Users = from userInfo in xdocument.Element("Root").Elements() select new { ID = userInfo.Element("UserID").Value, Total = userInfo.Element("Total").Value , Day = userInfo.Element("Day").Value }; foreach (var item in Users) { Ticket Obj = new Ticket(Convert.ToInt16(item.ID), Convert.ToDouble(item.Total), Convert.ToDateTime(item.Day)); ObjList.Add(Obj); } } else { var UserForWhere = from userInfo in xdocument.Element("Root").Elements() where Convert.ToInt32(userInfo.Element("UserID").Value) == ID select new { ID = userInfo.Element("UserID").Value, Total = userInfo.Element("Total").Value, Day = userInfo.Element("Day").Value }; foreach (var item in UserForWhere) { Ticket Obj = new Ticket(Convert.ToInt16(item.ID), Convert.ToDouble(item.Total), Convert.ToDateTime(item.Day)); ObjList.Add(Obj); } } return ObjList; }

按日期查询 需要查询的日期作为参数
public static List ReSearch(DateTime Day) { List ObjList = new List(); var UserForWhere = from userInfo in xdocument.Element("Root").Elements() where Convert.ToDateTime(userInfo.Element("Day").Value) == Day select new { ID = userInfo.Element("UserID").Value, Total = userInfo.Element("Total").Value, Day = userInfo.Element("Day").Value }; foreach (var item in UserForWhere) { Ticket Obj = new Ticket(Convert.ToInt16(item.ID), Convert.ToDouble(item.Total), Convert.ToDateTime(item.Day)); ObjList.Add(Obj); } return ObjList; }

成品如图
Winform对XML文件的增删改查
文章图片

前段UI包括对Datagridview的数据绑定以及对方法的引用,不再赘述。源代码感兴趣的可以自取
【Winform对XML文件的增删改查】链接:https://pan.baidu.com/s/1Zvddm673Gb7hu4kLy9WPCg
提取码:8alp

    推荐阅读