XMLHelper.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Xml.Linq;
  6. using DBConn;
  7. namespace Core.ONH.Collection.comm
  8. {
  9. public class XmlHelper
  10. {
  11. private static readonly object LockHelper = new object();
  12. public static void GenerateXmlFile(string xmlPath)
  13. {
  14. try
  15. {
  16. //定义一个XDocument结构
  17. XDocument myXDoc = new XDocument(
  18. new XElement("Root"
  19. )
  20. );
  21. myXDoc.Save(xmlPath);
  22. }
  23. catch (Exception ex)
  24. {
  25. Console.WriteLine(ex.ToString());
  26. }
  27. }
  28. public static string GetXmlData(string path, string attri, string defaultVaule = "")
  29. {
  30. lock (LockHelper)
  31. {
  32. if (!File.Exists(path))
  33. GenerateXmlFile(path);
  34. XElement rootNode = XElement.Load(path);
  35. var attribute = rootNode.Attributes().FirstOrDefault(p => p.Name == attri);
  36. return attribute != null ? attribute.Value : defaultVaule;
  37. }
  38. }
  39. public static double GetXmlData(string path, string attri, double defaultVaule = 0d)
  40. {
  41. lock (LockHelper)
  42. {
  43. if (!File.Exists(path))
  44. GenerateXmlFile(path);
  45. XElement rootNode = XElement.Load(path);
  46. var attribute = rootNode.Attributes().FirstOrDefault(p => p.Name == attri);
  47. double returnValue;
  48. return attribute == null || !double.TryParse(attribute.Value, out returnValue) ? defaultVaule : returnValue;
  49. }
  50. }
  51. public static int GetXmlData(string path, string attri, int defaultVaule = 0)
  52. {
  53. lock (LockHelper)
  54. {
  55. if (!File.Exists(path))
  56. GenerateXmlFile(path);
  57. XElement rootNode = XElement.Load(path);
  58. var attribute = rootNode.Attributes().FirstOrDefault(p => p.Name == attri);
  59. int returnValue;
  60. return attribute == null || !int.TryParse(attribute.Value, out returnValue) ? defaultVaule : returnValue;
  61. }
  62. }
  63. public static bool GetXmlData(string path, string attri, bool defaultVaule = false)
  64. {
  65. lock (LockHelper)
  66. {
  67. if (!File.Exists(path))
  68. GenerateXmlFile(path);
  69. XElement rootNode = XElement.Load(path);
  70. var attribute = rootNode.Attributes().FirstOrDefault(p => p.Name == attri);
  71. int returnValue;
  72. return attribute == null || !int.TryParse(attribute.Value, out returnValue) ? defaultVaule : returnValue==1;
  73. }
  74. }
  75. public static void SetXmlData(string path, string attri, object data)
  76. {
  77. lock (LockHelper)
  78. {
  79. if (!File.Exists(path))
  80. GenerateXmlFile(path);
  81. XElement rootNode = XElement.Load(path);
  82. rootNode.SetAttributeValue(attri, data);
  83. rootNode.Save(path);
  84. }
  85. }
  86. public static void AddNodes(string path, string xe, object data,string keyProperties,string KeyValue, List<string> WriteProperties = null )
  87. {
  88. lock (LockHelper)
  89. {
  90. if (!File.Exists(path))
  91. GenerateXmlFile(path);
  92. XElement rootNode = XElement.Load(path);
  93. XElement NewElement = new XElement(xe);
  94. foreach (var propertyInfo in data.GetType().GetProperties().AsParallel())
  95. {
  96. if (WriteProperties != null)
  97. {
  98. if (!WriteProperties.Contains(propertyInfo.Name))
  99. {
  100. continue;
  101. }
  102. NewElement.Add(new XAttribute(propertyInfo.Name, propertyInfo.GetValue(data, null).ToString()));
  103. }
  104. }
  105. if (keyProperties != "")
  106. {
  107. rootNode.Elements(xe).Where(p =>
  108. {
  109. var xAttribute = p.Attribute(keyProperties);
  110. return xAttribute != null && xAttribute.Value == KeyValue;
  111. }).ToList().ForEach(p => p.Remove());
  112. }
  113. rootNode.Add(NewElement);
  114. rootNode.Save(path);
  115. }
  116. }
  117. public static void ChangeNodes(string path, string xe, object data, string keyProperties, string KeyValue,
  118. List<string> WriteProperties = null)
  119. {
  120. lock (LockHelper)
  121. {
  122. if (!File.Exists(path))
  123. GenerateXmlFile(path);
  124. XElement rootNode = XElement.Load(path);
  125. var xElements = rootNode.Elements(xe).Where(p =>
  126. {
  127. var xAttribute = p.Attribute(keyProperties);
  128. return xAttribute != null && xAttribute.Value == KeyValue;
  129. }).ToList();
  130. if (xElements.Count <= 0)
  131. {
  132. AddNodes(path, xe, data, keyProperties, KeyValue, WriteProperties);
  133. return;
  134. }
  135. XElement NewElement = xElements[0];
  136. foreach (var propertyInfo in data.GetType().GetProperties().AsParallel())
  137. {
  138. if (WriteProperties != null)
  139. {
  140. if (!WriteProperties.Contains(propertyInfo.Name))
  141. {
  142. continue;
  143. }
  144. var atrr = NewElement.Attribute(propertyInfo.Name);
  145. if (atrr == null)
  146. {
  147. NewElement.Add(new XAttribute(propertyInfo.Name,
  148. propertyInfo.GetValue(data, null).ToString()));
  149. }
  150. else
  151. {
  152. atrr.SetValue(propertyInfo.GetValue(data, null).ToString());
  153. }
  154. }
  155. }
  156. rootNode.Save(path);
  157. }
  158. }
  159. public static void DeleteNodes(string path, string xe, string keyProperties, string KeyValue)
  160. {
  161. lock (LockHelper)
  162. {
  163. if (!File.Exists(path))
  164. GenerateXmlFile(path);
  165. XElement rootNode = XElement.Load(path);
  166. if (keyProperties != "")
  167. {
  168. rootNode.Elements(xe).Where(p =>
  169. {
  170. var xAttribute = p.Attribute(keyProperties);
  171. return xAttribute != null && xAttribute.Value == KeyValue;
  172. }).ToList().ForEach(p => p.Remove());
  173. }
  174. rootNode.Save(path);
  175. }
  176. }
  177. public static List<Dictionary<string, string>> GetXmlAttributes(string path, string Node)
  178. {
  179. lock (LockHelper)
  180. {
  181. if (!File.Exists(path))
  182. GenerateXmlFile(path);
  183. XElement rootNode = XElement.Load(path);
  184. List<Dictionary<string,string>> datas = new List<Dictionary<string, string>>();
  185. foreach (var Element in rootNode.Elements(Node))
  186. {
  187. Dictionary<string,string> data = new Dictionary<string, string>();
  188. foreach (var Attribute in Element.Attributes())
  189. {
  190. data.Add(Attribute.Name.ToString(),Attribute.Value);
  191. }
  192. datas.Add(data);
  193. }
  194. return datas;
  195. }
  196. }
  197. public static List<T> GetXmlAttributes<T>(string path, string Node) where T : new()
  198. {
  199. lock (LockHelper)
  200. {
  201. if (!File.Exists(path))
  202. GenerateXmlFile(path);
  203. XElement rootNode = XElement.Load(path);
  204. return new ModelDbHandler<T>().GetModelByElements(rootNode.Elements(Node).ToList());
  205. }
  206. }
  207. }
  208. }