CustomXmlSerializer.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. XmlWriterSettings settings = new XmlWriterSettings
  71. {
  72. Indent = true,
  73. IndentChars = " "
  74. };
  75. using (FileStream fileWriter = new FileStream(fileName, FileMode.Create))
  76. using (XmlWriter writer = XmlTextWriter.Create(fileWriter, settings))
  77. {
  78. XmlSerializer serializer = new XmlSerializer(t.GetType());
  79. serializer.Serialize(writer, t);
  80. writer.Flush();
  81. }
  82. }
  83. static public void SerializeA<T>(T t, string fileName)
  84. {
  85. StringBuilder buffer = new StringBuilder();
  86. using (StringWriter writer = new StringWriter(buffer))
  87. {
  88. XmlSerializer serializer = new XmlSerializer(typeof(T));
  89. serializer.Serialize(writer, t);
  90. }
  91. var data= buffer.ToString();
  92. }
  93. static public string Serialize<T>(T t)
  94. {
  95. StringBuilder buffer = new StringBuilder();
  96. using (StringWriter writer = new StringWriter(buffer))
  97. {
  98. XmlSerializer serializer = new XmlSerializer(typeof(T));
  99. serializer.Serialize(writer, t);
  100. }
  101. return buffer.ToString();
  102. }
  103. static public T Deserialize<T>(string xmlString)
  104. {
  105. if (string.IsNullOrWhiteSpace(xmlString))
  106. throw new ArgumentNullException("xmlString");
  107. using (StringReader reader = new StringReader(xmlString))
  108. {
  109. XmlSerializer serializer = new XmlSerializer(typeof(T));
  110. return (T)serializer.Deserialize(reader);
  111. }
  112. }
  113. static public 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. static public 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. }