using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Xml.Linq; using DBConn; namespace Core.ONH.Collection.comm { public class XmlHelper { private static readonly object LockHelper = new object(); public static void GenerateXmlFile(string xmlPath) { try { //定义一个XDocument结构 XDocument myXDoc = new XDocument( new XElement("Root" ) ); myXDoc.Save(xmlPath); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } public static string GetXmlData(string path, string attri, string defaultVaule = "") { lock (LockHelper) { if (!File.Exists(path)) GenerateXmlFile(path); XElement rootNode = XElement.Load(path); var attribute = rootNode.Attributes().FirstOrDefault(p => p.Name == attri); return attribute != null ? attribute.Value : defaultVaule; } } public static double GetXmlData(string path, string attri, double defaultVaule = 0d) { lock (LockHelper) { if (!File.Exists(path)) GenerateXmlFile(path); XElement rootNode = XElement.Load(path); var attribute = rootNode.Attributes().FirstOrDefault(p => p.Name == attri); double returnValue; return attribute == null || !double.TryParse(attribute.Value, out returnValue) ? defaultVaule : returnValue; } } public static int GetXmlData(string path, string attri, int defaultVaule = 0) { lock (LockHelper) { if (!File.Exists(path)) GenerateXmlFile(path); XElement rootNode = XElement.Load(path); var attribute = rootNode.Attributes().FirstOrDefault(p => p.Name == attri); int returnValue; return attribute == null || !int.TryParse(attribute.Value, out returnValue) ? defaultVaule : returnValue; } } public static bool GetXmlData(string path, string attri, bool defaultVaule = false) { lock (LockHelper) { if (!File.Exists(path)) GenerateXmlFile(path); XElement rootNode = XElement.Load(path); var attribute = rootNode.Attributes().FirstOrDefault(p => p.Name == attri); int returnValue; return attribute == null || !int.TryParse(attribute.Value, out returnValue) ? defaultVaule : returnValue==1; } } public static void SetXmlData(string path, string attri, object data) { lock (LockHelper) { if (!File.Exists(path)) GenerateXmlFile(path); XElement rootNode = XElement.Load(path); rootNode.SetAttributeValue(attri, data); rootNode.Save(path); } } public static void AddNodes(string path, string xe, object data,string keyProperties,string KeyValue, List WriteProperties = null ) { lock (LockHelper) { if (!File.Exists(path)) GenerateXmlFile(path); XElement rootNode = XElement.Load(path); XElement NewElement = new XElement(xe); foreach (var propertyInfo in data.GetType().GetProperties().AsParallel()) { if (WriteProperties != null) { if (!WriteProperties.Contains(propertyInfo.Name)) { continue; } NewElement.Add(new XAttribute(propertyInfo.Name, propertyInfo.GetValue(data, null).ToString())); } } if (keyProperties != "") { rootNode.Elements(xe).Where(p => { var xAttribute = p.Attribute(keyProperties); return xAttribute != null && xAttribute.Value == KeyValue; }).ToList().ForEach(p => p.Remove()); } rootNode.Add(NewElement); rootNode.Save(path); } } public static void ChangeNodes(string path, string xe, object data, string keyProperties, string KeyValue, List WriteProperties = null) { lock (LockHelper) { if (!File.Exists(path)) GenerateXmlFile(path); XElement rootNode = XElement.Load(path); var xElements = rootNode.Elements(xe).Where(p => { var xAttribute = p.Attribute(keyProperties); return xAttribute != null && xAttribute.Value == KeyValue; }).ToList(); if (xElements.Count <= 0) { AddNodes(path, xe, data, keyProperties, KeyValue, WriteProperties); return; } XElement NewElement = xElements[0]; foreach (var propertyInfo in data.GetType().GetProperties().AsParallel()) { if (WriteProperties != null) { if (!WriteProperties.Contains(propertyInfo.Name)) { continue; } var atrr = NewElement.Attribute(propertyInfo.Name); if (atrr == null) { NewElement.Add(new XAttribute(propertyInfo.Name, propertyInfo.GetValue(data, null).ToString())); } else { atrr.SetValue(propertyInfo.GetValue(data, null).ToString()); } } } rootNode.Save(path); } } public static void DeleteNodes(string path, string xe, string keyProperties, string KeyValue) { lock (LockHelper) { if (!File.Exists(path)) GenerateXmlFile(path); XElement rootNode = XElement.Load(path); if (keyProperties != "") { rootNode.Elements(xe).Where(p => { var xAttribute = p.Attribute(keyProperties); return xAttribute != null && xAttribute.Value == KeyValue; }).ToList().ForEach(p => p.Remove()); } rootNode.Save(path); } } public static List> GetXmlAttributes(string path, string Node) { lock (LockHelper) { if (!File.Exists(path)) GenerateXmlFile(path); XElement rootNode = XElement.Load(path); List> datas = new List>(); foreach (var Element in rootNode.Elements(Node)) { Dictionary data = new Dictionary(); foreach (var Attribute in Element.Attributes()) { data.Add(Attribute.Name.ToString(),Attribute.Value); } datas.Add(data); } return datas; } } public static List GetXmlAttributes(string path, string Node) where T : new() { lock (LockHelper) { if (!File.Exists(path)) GenerateXmlFile(path); XElement rootNode = XElement.Load(path); return new ModelDbHandler().GetModelByElements(rootNode.Elements(Node).ToList()); } } } }