【IT168技术文档】
在列举示例代码之前,先列举在最新的版本中新增的一个类SerializeHelper.cs的代码,该类封装简化了常用的序列化方法,并且将在示例中被反复调用。
SerializeHelper.cs
1using System; 2using System.Collections; 3using System.IO; 4using System.Text; 5using System.Collections.Generic; 6using System.Xml; 7using System.Xml.Serialization; 8using System.Runtime.Serialization; 9using System.Runtime.Serialization.Formatters.Soap; 10using System.Runtime.Serialization.Formatters.Binary; 11 12namespace Ilungasoft.Framework.Common 13{ 14 public class SerializeHelper 15 { 16 private static string RemoveXmlHeader(string xml) 17 { 18 if (xml.StartsWith("<?")) 19 { 20 return xml.Substring(xml.IndexOf("?>") + 2); 21 } 22 else 23 { 24 return xml; 25 } 26 } 27 28 public static string Serialize(object obj) 29 { 30 if (obj == null) 31 { 32 return string.Empty; 33 } 34 35 XmlSerializer s = new XmlSerializer(obj.GetType()); 36 StringBuilder sb = new StringBuilder(); 37 s.Serialize(new StringWriter(sb), obj); 38 return RemoveXmlHeader(sb.ToString()); 39 } 40 41 public static void Serialize(Stream stream, object obj) 42 { 43 if (obj == null) 44 { 45 return; 46 } 47 48 XmlSerializer s = new XmlSerializer(obj.GetType()); 49 s.Serialize(stream, obj); 50 } 51 52 public static string SerializeArray(object[] objs) 53 { 54 if (objs != null && objs.Length > 0) 55 { 56 XmlSerializer s = new XmlSerializer(objs[0].GetType().MakeArrayType()); 57 StringBuilder sb = new StringBuilder(); 58 s.Serialize(new StringWriter(sb), objs); 59 return RemoveXmlHeader(sb.ToString()); 60 } 61 else 62 { 63 return string.Empty; 64 } 65 } 66 67 public static void SerializeArray(Stream stream, object[] objs) 68 { 69 if (objs != null && objs.Length > 0) 70 { 71 XmlSerializer s = new XmlSerializer(objs[0].GetType().MakeArrayType()); 72 s.Serialize(stream, objs); 73 } 74 else 75 { 76 return; 77 } 78 } 79 80 public static ReturnType Deserialize<ReturnType>(Type originType, string xml) 81 { 82 XmlSerializer s = new XmlSerializer(originType); 83 return (ReturnType)s.Deserialize(new StringReader(xml)); 84 } 85 86 public static string SoapSerialize(object obj) 87 { 88 SoapFormatter formatter = new SoapFormatter(); 89 MemoryStream stream = new MemoryStream(); 90 formatter.Serialize(stream, obj); 91 return System.Text.UTF8Encoding.UTF8.GetString(stream.ToArray()); 92 } 93 94 public static void SoapSerialize(Stream stream, object obj) 95 { 96 SoapFormatter formatter = new SoapFormatter(); 97 formatter.Serialize(stream, obj); 98 } 99 100 public static ReturnType SoapDeserialize<ReturnType>(string xml) 101 { 102 SoapFormatter formatter = new SoapFormatter(); 103 MemoryStream stream = new MemoryStream(System.Text.UTF8Encoding.UTF8.GetBytes(xml)); 104 return (ReturnType)formatter.Deserialize(stream); 105 } 106 107 public static ReturnType SoapDeserialize<ReturnType>(Stream stream) 108 { 109 SoapFormatter formatter = new SoapFormatter(); 110 return (ReturnType)formatter.Deserialize(stream); 111 } 112 113 public static FieldType LoadField<FieldType>(string elementName, SerializationInfo info) 114 where FieldType : IEntity 115 { 116 string xml = info.GetString(elementName); 117 return xml == null ? default(FieldType) : (FieldType)SerializeHelper.Deserialize<FieldType>(EntityFactory<FieldType>.GetDynamicEntityType(), xml); 118 } 119 120 public static ItemType[] LoadArrayField<ItemType>(string elementName, SerializationInfo info) 121 where ItemType : IEntity 122 { 123 string xml = info.GetString(elementName); 124 return xml == null ? default(ItemType[]) : SerializeHelper.Deserialize<ItemType[]>(EntityFactory<ItemType>.GetDynamicEntityType().MakeArrayType(), xml); 125 } 126 127 public static void SaveField(string elementName, object value, SerializationInfo info) 128 { 129 if (value != null) 130 { 131 info.AddValue(elementName, SerializeHelper.Serialize(value)); 132 } 133 } 134 135 public static void SaveArrayField(string elementName, object[] values, SerializationInfo info) 136 { 137 if (values != null) 138 { 139 info.AddValue(elementName, SerializeHelper.SerializeArray(values)); 140 } 141 } 142 } 143}