XmlHelper.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Xml.Linq;
  7. using System.Xml.Serialization;
  8. namespace Core.StlMes.Client.LgResMgt.Mcms
  9. {
  10. class XmlHelper
  11. {
  12. private static readonly object LockHelper = new object();
  13. private static void GenerateXmlFile(string xmlPath)
  14. {
  15. try
  16. {
  17. //定义一个XDocument结构
  18. var myXDoc = new XDocument(
  19. new XElement("Root"
  20. )
  21. );
  22. myXDoc.Save(xmlPath);
  23. }
  24. catch (Exception ex)
  25. {
  26. Console.WriteLine(ex.ToString());
  27. }
  28. }
  29. public static string GetXmlData(string path, string attri, string defaultVaule = "")
  30. {
  31. lock (LockHelper)
  32. {
  33. if (!File.Exists(path))
  34. GenerateXmlFile(path);
  35. var rootNode = XElement.Load(path);
  36. var attribute = rootNode.Attributes().FirstOrDefault(p => p.Name == attri);
  37. return attribute != null ? attribute.Value : defaultVaule;
  38. }
  39. }
  40. public static string GetAttribute(XElement xl, string attri, string defaultVaule = "")
  41. {
  42. var attribute = xl.Attributes().FirstOrDefault(p => p.Name == attri);
  43. return attribute != null ? attribute.Value : defaultVaule;
  44. }
  45. public static double GetXmlData(string path, string attri, double defaultVaule = 0d)
  46. {
  47. lock (LockHelper)
  48. {
  49. if (!File.Exists(path))
  50. GenerateXmlFile(path);
  51. var rootNode = XElement.Load(path);
  52. var attribute = rootNode.Attributes().FirstOrDefault(p => p.Name == attri);
  53. double returnValue;
  54. return (attribute == null) || !double.TryParse(attribute.Value, out returnValue)
  55. ? defaultVaule
  56. : returnValue;
  57. }
  58. }
  59. public static int GetXmlData(string path, string attri, int defaultVaule = 0)
  60. {
  61. lock (LockHelper)
  62. {
  63. if (!File.Exists(path))
  64. GenerateXmlFile(path);
  65. var rootNode = XElement.Load(path);
  66. var attribute = rootNode.Attributes().FirstOrDefault(p => p.Name == attri);
  67. int returnValue;
  68. return (attribute == null) || !int.TryParse(attribute.Value, out returnValue)
  69. ? defaultVaule
  70. : returnValue;
  71. }
  72. }
  73. public static void SetXmlData(string path, string attri, object data)
  74. {
  75. lock (LockHelper)
  76. {
  77. if (!File.Exists(path))
  78. GenerateXmlFile(path);
  79. var rootNode = XElement.Load(path);
  80. rootNode.SetAttributeValue(attri, data);
  81. rootNode.Save(path);
  82. }
  83. }
  84. public static void SetXElement(string path, XElement data,bool recover )
  85. {
  86. lock (LockHelper)
  87. {
  88. if (!File.Exists(path))
  89. GenerateXmlFile(path);
  90. var rootNode = XElement.Load(path);
  91. if (recover)
  92. {
  93. foreach (var xElement in rootNode.Elements().Where(p=>p.Name.ToString3()==data.Name.ToString3()))
  94. {
  95. xElement.Remove();
  96. }
  97. }
  98. rootNode.Add(data);
  99. rootNode.Save(path);
  100. }
  101. }
  102. public static XElement GetXElement(string path, string name)
  103. {
  104. lock (LockHelper)
  105. {
  106. if (!File.Exists(path))
  107. {
  108. return null;
  109. }
  110. var rootNode = XElement.Load(path);
  111. return rootNode.Element(name);
  112. }
  113. }
  114. public static void SetXmlEntityData<T>(string path, T data)
  115. {
  116. lock (LockHelper)
  117. {
  118. try
  119. {
  120. var myXDoc = new XDocument();
  121. myXDoc.Add(ToXElement(data));
  122. myXDoc.Save(path);
  123. }
  124. catch (Exception ex)
  125. {
  126. Console.WriteLine(ex.ToString());
  127. }
  128. }
  129. }
  130. public static void SetXmlEntityData<T>(string path,string node, T data)
  131. {
  132. lock (LockHelper)
  133. {
  134. try
  135. {
  136. var rootNode = XElement.Load(path);
  137. rootNode.Add(ToXElement(data));
  138. rootNode.Save(path);
  139. }
  140. catch (Exception ex)
  141. {
  142. Console.WriteLine(ex.ToString());
  143. }
  144. }
  145. }
  146. public static T FromXElement<T>(XElement xElement)
  147. {
  148. var xmlSerializer = new XmlSerializer(typeof(T));
  149. return (T)xmlSerializer.Deserialize(xElement.CreateReader());
  150. }
  151. public static T GetXmlData<T>(string path) where T : new()
  152. {
  153. lock (LockHelper)
  154. {
  155. if (!File.Exists(path))
  156. {
  157. return new T();
  158. }
  159. var rootNode = XElement.Load(path);
  160. return FromXElement<T>(rootNode);
  161. }
  162. }
  163. private static T SetEntity<T>(List<XElement> list) where T : new()
  164. {
  165. T entity = new T();
  166. foreach (var propertyInfo in typeof(T).GetProperties())
  167. {
  168. var info = propertyInfo;
  169. var data = list.FirstOrDefault(p => p.Name == info.Name);
  170. if (data != null)
  171. {
  172. if (propertyInfo.PropertyType.FullName != null && propertyInfo.PropertyType.FullName.StartsWith("System."))
  173. {
  174. propertyInfo.SetValue(entity, data.Value, null);
  175. }
  176. else
  177. {
  178. /* Type rType = propertyInfo.PropertyType;
  179. propertyInfo.SetValue(entity, SetEntity<rType>()
  180. , null);*/
  181. }
  182. }
  183. }
  184. return entity;
  185. }
  186. public static XElement ToXElement<T>(T obj)
  187. {
  188. using (var memoryStream = new MemoryStream())
  189. {
  190. using (TextWriter streamWriter = new StreamWriter(memoryStream))
  191. {
  192. var xmlSerializer = new XmlSerializer(typeof(T));
  193. xmlSerializer.Serialize(streamWriter, obj);
  194. return XElement.Parse(Encoding.UTF8.GetString(memoryStream.ToArray()));
  195. }
  196. }
  197. }
  198. }
  199. }