CustomXmlSerializer.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Runtime.Serialization.Formatters.Soap;
  6. using System.Text;
  7. using System.IO;
  8. using System.Xml.Serialization;
  9. using System.Xml;
  10. namespace Aitex.Core.Util
  11. {
  12. public class SerializeStatic
  13. {
  14. public static bool Save(Type static_class, string filename)
  15. {
  16. try
  17. {
  18. FieldInfo[] fields = static_class.GetFields(BindingFlags.Static | BindingFlags.Public);
  19. object[,] a = new object[fields.Length, 2];
  20. int i = 0;
  21. foreach (FieldInfo field in fields)
  22. {
  23. a[i, 0] = field.Name;
  24. a[i, 1] = field.GetValue(null);
  25. i++;
  26. };
  27. Stream f = File.Open(filename, FileMode.Create);
  28. SoapFormatter formatter = new SoapFormatter();
  29. formatter.Serialize(f, a);
  30. f.Close();
  31. return true;
  32. }
  33. catch
  34. {
  35. return false;
  36. }
  37. }
  38. public static bool Load(Type static_class, string filename)
  39. {
  40. try
  41. {
  42. FieldInfo[] fields = static_class.GetFields(BindingFlags.Static | BindingFlags.Public);
  43. object[,] a;
  44. Stream f = File.Open(filename, FileMode.Open);
  45. SoapFormatter formatter = new SoapFormatter();
  46. a = formatter.Deserialize(f) as object[,];
  47. f.Close();
  48. if (a.GetLength(0) != fields.Length) return false;
  49. int i = 0;
  50. foreach (FieldInfo field in fields)
  51. {
  52. if (field.Name == (a[i, 0] as string))
  53. {
  54. field.SetValue(null, a[i, 1]);
  55. }
  56. i++;
  57. };
  58. return true;
  59. }
  60. catch
  61. {
  62. return false;
  63. }
  64. }
  65. }
  66. public class CustomXmlSerializer
  67. {
  68. public static void Serialize<T>(T t, string fileName)
  69. {
  70. using (FileStream fileWriter = new FileStream(fileName, FileMode.Create))
  71. using (XmlWriter writer = XmlTextWriter.Create(fileWriter))
  72. {
  73. XmlSerializer serializer = new XmlSerializer(t.GetType());
  74. serializer.Serialize(writer, t);
  75. writer.Flush();
  76. }
  77. }
  78. public static string Serialize<T>(T t)
  79. {
  80. StringBuilder buffer = new StringBuilder();
  81. using (StringWriter writer = new StringWriter(buffer))
  82. {
  83. XmlSerializer serializer = new XmlSerializer(typeof(T));
  84. serializer.Serialize(writer, t);
  85. }
  86. return buffer.ToString();
  87. }
  88. public static T Deserialize<T>(string xmlString)
  89. {
  90. if (string.IsNullOrWhiteSpace(xmlString))
  91. throw new ArgumentNullException("xmlString");
  92. using (StringReader reader = new StringReader(xmlString))
  93. {
  94. XmlSerializer serializer = new XmlSerializer(typeof(T));
  95. return (T)serializer.Deserialize(reader);
  96. }
  97. }
  98. /// <summary>
  99. /// 加载
  100. /// </summary>
  101. /// <param name="xmlPath"></param>
  102. /// <returns></returns>
  103. public static T LoadXmlFile<T>(string xmlPath)
  104. {
  105. if (string.IsNullOrWhiteSpace(xmlPath))
  106. throw new ArgumentNullException("xmplPath");
  107. var serializer = new XmlSerializer(typeof(T));
  108. using (var stream = new FileStream(xmlPath, FileMode.Open))
  109. {
  110. return (T)serializer.Deserialize(stream);
  111. }
  112. }
  113. public static T Deserialize<T>(Stream streamReader)
  114. {
  115. if (streamReader == null)
  116. throw new ArgumentNullException("streamReader");
  117. if (streamReader.Length <= 0)
  118. throw new EndOfStreamException("The stream is blank");
  119. using (XmlReader reader = XmlTextReader.Create(streamReader))
  120. {
  121. XmlSerializer deserializer = new XmlSerializer(typeof(T));
  122. return (T)deserializer.Deserialize(reader);
  123. }
  124. }
  125. public static T Deserialize<T>(FileInfo fi)
  126. {
  127. if (fi == null)
  128. throw new ArgumentNullException("fi");
  129. if (!fi.Exists)
  130. throw new FileNotFoundException(fi.FullName);
  131. using (FileStream fileReader = new FileStream(fi.FullName, FileMode.Open, FileAccess.Read))
  132. return Deserialize<T>(fileReader);
  133. }
  134. }
  135. }