CustomXmlSerializer.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. static public 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. static public 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. static public 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. static public T Deserialize<T>(Stream streamReader)
  99. {
  100. if (streamReader == null)
  101. throw new ArgumentNullException("streamReader");
  102. if (streamReader.Length <= 0)
  103. throw new EndOfStreamException("The stream is blank");
  104. using (XmlReader reader = XmlTextReader.Create(streamReader))
  105. {
  106. XmlSerializer deserializer = new XmlSerializer(typeof(T));
  107. return (T)deserializer.Deserialize(reader);
  108. }
  109. }
  110. static public T Deserialize<T>(FileInfo fi)
  111. {
  112. if (fi == null)
  113. throw new ArgumentNullException("fi");
  114. if (!fi.Exists)
  115. throw new FileNotFoundException(fi.FullName);
  116. using (FileStream fileReader = new FileStream(fi.FullName, FileMode.Open, FileAccess.Read))
  117. return Deserialize<T>(fileReader);
  118. }
  119. }
  120. }