XmlHelper.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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.Mcp.Mch.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 double GetXmlData(string path, string attri, double defaultVaule = 0d)
  41. {
  42. lock (LockHelper)
  43. {
  44. if (!File.Exists(path))
  45. GenerateXmlFile(path);
  46. var rootNode = XElement.Load(path);
  47. var attribute = rootNode.Attributes().FirstOrDefault(p => p.Name == attri);
  48. double returnValue;
  49. return (attribute == null) || !double.TryParse(attribute.Value, out returnValue)
  50. ? defaultVaule
  51. : returnValue;
  52. }
  53. }
  54. public static int GetXmlData(string path, string attri, int defaultVaule = 0)
  55. {
  56. lock (LockHelper)
  57. {
  58. if (!File.Exists(path))
  59. GenerateXmlFile(path);
  60. var rootNode = XElement.Load(path);
  61. var attribute = rootNode.Attributes().FirstOrDefault(p => p.Name == attri);
  62. int returnValue;
  63. return (attribute == null) || !int.TryParse(attribute.Value, out returnValue)
  64. ? defaultVaule
  65. : returnValue;
  66. }
  67. }
  68. public static void SetXmlData(string path, string attri, object data)
  69. {
  70. lock (LockHelper)
  71. {
  72. if (!File.Exists(path))
  73. GenerateXmlFile(path);
  74. var rootNode = XElement.Load(path);
  75. rootNode.SetAttributeValue(attri, data);
  76. rootNode.Save(path);
  77. }
  78. }
  79. public static void SetXmlEntityData<T>(string path, T data)
  80. {
  81. lock (LockHelper)
  82. {
  83. try
  84. {
  85. var myXDoc = new XDocument();
  86. myXDoc.Add(ToXElement(data));
  87. myXDoc.Save(path);
  88. }
  89. catch (Exception ex)
  90. {
  91. Console.WriteLine(ex.ToString());
  92. }
  93. }
  94. }
  95. public static void SetXmlEntityData<T>(string path,string node, T data)
  96. {
  97. lock (LockHelper)
  98. {
  99. try
  100. {
  101. var rootNode = XElement.Load(path);
  102. rootNode.Add(ToXElement(data));
  103. rootNode.Save(path);
  104. }
  105. catch (Exception ex)
  106. {
  107. Console.WriteLine(ex.ToString());
  108. }
  109. }
  110. }
  111. public static T FromXElement<T>(XElement xElement)
  112. {
  113. var xmlSerializer = new XmlSerializer(typeof(T));
  114. return (T)xmlSerializer.Deserialize(xElement.CreateReader());
  115. }
  116. public static T GetXmlData<T>(string path) where T : new()
  117. {
  118. lock (LockHelper)
  119. {
  120. if (!File.Exists(path))
  121. {
  122. return new T();
  123. }
  124. var rootNode = XElement.Load(path);
  125. return FromXElement<T>(rootNode);
  126. }
  127. }
  128. private static T SetEntity<T>(List<XElement> list) where T : new()
  129. {
  130. T entity = new T();
  131. foreach (var propertyInfo in typeof(T).GetProperties())
  132. {
  133. var info = propertyInfo;
  134. var data = list.FirstOrDefault(p => p.Name == info.Name);
  135. if (data != null)
  136. {
  137. if (propertyInfo.PropertyType.FullName != null && propertyInfo.PropertyType.FullName.StartsWith("System."))
  138. {
  139. propertyInfo.SetValue(entity, data.Value, null);
  140. }
  141. else
  142. {
  143. /* Type rType = propertyInfo.PropertyType;
  144. propertyInfo.SetValue(entity, SetEntity<rType>()
  145. , null);*/
  146. }
  147. }
  148. }
  149. return entity;
  150. }
  151. public static XElement ToXElement<T>(T obj)
  152. {
  153. using (var memoryStream = new MemoryStream())
  154. {
  155. using (TextWriter streamWriter = new StreamWriter(memoryStream))
  156. {
  157. var xmlSerializer = new XmlSerializer(typeof(T));
  158. xmlSerializer.Serialize(streamWriter, obj);
  159. return XElement.Parse(Encoding.UTF8.GetString(memoryStream.ToArray()));
  160. }
  161. }
  162. }
  163. }
  164. }