using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Xml.Linq; using System.Xml.Serialization; namespace Core.StlMes.Client.Mcp.Mch.Mcms { class XmlHelper { private static readonly object LockHelper = new object(); private static void GenerateXmlFile(string xmlPath) { try { //定义一个XDocument结构 var 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); var 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); var 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); var 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 void SetXmlData(string path, string attri, object data) { lock (LockHelper) { if (!File.Exists(path)) GenerateXmlFile(path); var rootNode = XElement.Load(path); rootNode.SetAttributeValue(attri, data); rootNode.Save(path); } } public static void SetXmlEntityData(string path, T data) { lock (LockHelper) { try { var myXDoc = new XDocument(); myXDoc.Add(ToXElement(data)); myXDoc.Save(path); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } } public static void SetXmlEntityData(string path,string node, T data) { lock (LockHelper) { try { var rootNode = XElement.Load(path); rootNode.Add(ToXElement(data)); rootNode.Save(path); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } } public static T FromXElement(XElement xElement) { var xmlSerializer = new XmlSerializer(typeof(T)); return (T)xmlSerializer.Deserialize(xElement.CreateReader()); } public static T GetXmlData(string path) where T : new() { lock (LockHelper) { if (!File.Exists(path)) { return new T(); } var rootNode = XElement.Load(path); return FromXElement(rootNode); } } private static T SetEntity(List list) where T : new() { T entity = new T(); foreach (var propertyInfo in typeof(T).GetProperties()) { var info = propertyInfo; var data = list.FirstOrDefault(p => p.Name == info.Name); if (data != null) { if (propertyInfo.PropertyType.FullName != null && propertyInfo.PropertyType.FullName.StartsWith("System.")) { propertyInfo.SetValue(entity, data.Value, null); } else { /* Type rType = propertyInfo.PropertyType; propertyInfo.SetValue(entity, SetEntity() , null);*/ } } } return entity; } public static XElement ToXElement(T obj) { using (var memoryStream = new MemoryStream()) { using (TextWriter streamWriter = new StreamWriter(memoryStream)) { var xmlSerializer = new XmlSerializer(typeof(T)); xmlSerializer.Serialize(streamWriter, obj); return XElement.Parse(Encoding.UTF8.GetString(memoryStream.ToArray())); } } } } }