写得比较啰嗦,自己记载备用
1 public class XmlFunction 2 { 3 private static XDocument _doc = new XDocument(); 4 public static string FilePath = "mydata.xml"; 5 public XmlFunction() 6 { 7 _doc = XDocument.Load(FilePath); 8 } 9 public XmlFunction(string filepath) 10 : this() 11 { 12 FilePath = filepath; 13 } 14 15 public static List<DataModel> ReadAll() 16 { 17 var list = new List<DataModel>(); 18 var xmllist = _doc.Descendants("datas").Elements("Jijin"); 19 foreach (var element in xmllist) 20 { 21 var dataModel = new DataModel(); 22 var xElement1 = element.Element("daima"); 23 if (xElement1 != null) 24 dataModel.Daima = xElement1.Value == "" ? "" : xElement1.Value; 25 var xElement2 = element.Element("Chicangliang"); 26 if (xElement2 != null) 27 dataModel.Chicangliang = xElement2.Value == "" ? "" : xElement2.Value; 28 var xElement3 = element.Element("Mairujia"); 29 if (xElement3 != null) 30 dataModel.Mairujia = xElement3.Value == "" ? "" : xElement3.Value; 31 list.Add(dataModel); 32 } 33 return list; 34 } 35 36 public DataModel Readone(string daima) 37 { 38 var selectItem = _doc.Descendants("Jijin").FirstOrDefault(p => 39 { 40 var element = p.Element("daima"); 41 return element != null && element.Value == daima; 42 }); 43 var model = new DataModel(); 44 if (selectItem == null) return model; 45 model.Daima = daima; 46 var chicangliang = ""; 47 var mairujia = ""; 48 var sichicangliang = selectItem.Element("Chicangliang"); 49 var simairujia = selectItem.Element("Mairujia"); 50 if (sichicangliang != null) 51 { 52 if (!string.IsNullOrEmpty(sichicangliang.Value)) 53 chicangliang = sichicangliang.Value; 54 } 55 if (simairujia != null) 56 { 57 if (!string.IsNullOrEmpty(simairujia.Value)) 58 mairujia = simairujia.Value; 59 } 60 model.Chicangliang = chicangliang; 61 model.Mairujia = mairujia; 62 return model; 63 } 64 65 public static bool Insert(DataModel model) 66 { 67 var db = new XElement("Jijin", 68 new XElement("daima", model.Daima), 69 new XElement("Chicangliang", model.Chicangliang), 70 new XElement("Mairujia", model.Mairujia) 71 ); 72 var element = _doc.Element("datas"); 73 if (element == null) return false; 74 element.Add(db); 75 _doc.Save(FilePath); 76 return true; 77 } 78 79 public static bool Delete(string path, string node, string daima) 80 { 81 var selectItem = _doc.Descendants("Jijin").FirstOrDefault(p => 82 { 83 var element = p.Element("daima"); 84 return element != null && element.Value == daima; 85 }); 86 if (selectItem == null) return false; 87 selectItem.Remove(); 88 _doc.Save(FilePath); 89 return true; 90 } 91 92 public static bool Modify(DataModel model) 93 { 94 var selectItem = _doc.Descendants("datas").Elements("Jijin") 95 .FirstOrDefault(p => 96 { 97 var element = p.Element("daima"); 98 return element != null && element.Value == model.Daima; 99 }); if (selectItem == null) return false; var element1 = selectItem.Element("daima"); if (element1 != null) element1.Value = model.Daima; var element2 = selectItem.Element("Chicangliang"); if (element2 != null) element2.Value = model.Chicangliang; var element3 = selectItem.Element("Mairujia"); if (element3 != null) element3.Value = model.Mairujia; _doc.Save(FilePath); return true; } }