public MyXmlSerializer(Type clrType) { type = clrType; XmlAttributeOverrides overrides = ObjectEx.GetXmlOverrides(clrType); XmlSerializer = new XmlSerializer(clrType, overrides); Locker = new object(); IsLock = false; }
/// <summary> /// 返回一个对象的序列化流 /// </summary> /// <param name="source"></param> /// <returns></returns> public static MemoryStream Serialized(object source) { if (source == null) { return(null); } XmlObjectSerializer serializer = ObjectEx.GetXmlObjectSerializer(source.GetType()); return(Serialized(source, serializer)); }
/// <summary> /// 从压缩的流中读取实体列表。 /// </summary> /// <param name="stream"></param> /// <param name="dtoType"></param> /// <returns></returns> public static IList GetCompressedEntities(Stream stream, Type dtoType) { if (stream == null) { return(new List <object>()); } MemoryStream data; data = FileOperator.Decompressed(stream); return(ObjectEx.Deserialized(data, typeof(IList <>).MakeGenericType(dtoType)) as IList); }
public static T ReadObject <T>(string xmlData) { if (xmlData.IsNullOrWhiteSpace()) { return(default(T)); } XmlObjectSerializer serializer = ObjectEx.GetXmlObjectSerializer(typeof(T)); using (StringReader strread = new StringReader(xmlData)) { using (XmlReader reader = XmlReader.Create(strread)) { return((T)serializer.ReadObject(reader)); } } }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="item"></param> /// <returns></returns> public static string ToMsgXml <T>(this T item) where T : class { if (item == null) { return(null); } Type t = item.GetType(); XmlAttributeOverrides overrides = ObjectEx.GetXmlOverrides(t); XmlSerializer serializer = new XmlSerializer(t, overrides); StringBuilder sb = new StringBuilder(); using (XmlWriter writer = XmlWriter.Create(sb, XmlEx.WriterSettings)) { serializer.Serialize(writer, item); } return(sb.ToString()); }
/// <summary> /// 将一个流反序列化为指定类型的对象 /// </summary> /// <param name="source"></param> /// <param name="type"></param> /// <returns></returns> public static object Deserialized(Stream source, Type type, bool hideException) { if (source == null) { return(null); } if (type == typeof(XElement)) { source.Position = 0; using (XmlReader reader = XmlReader.Create(source)) { return(XElement.Load(reader)); } } else { XmlObjectSerializer serializer = ObjectEx.GetXmlObjectSerializer(type); return(Deserialized(source, serializer, hideException)); } }
/// <summary> /// 块缓存 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="items"></param> /// <param name="toStream"></param> /// <param name="blockSize"></param> /// <param name="isCompressed"></param> /// <returns></returns> public static int SerializeByBlock <T>(ICollection <T> items, Stream toStream, int blockSize, bool isCompressed) where T : class { IList blkItems; int i = 0; if (blockSize <= 0) { blockSize = int.MaxValue; } while (true) { blkItems = items.Skip(i * blockSize).Take(blockSize).ToList(); if (blkItems.Count == 0) { break; } using (MemoryStream ms = ObjectEx.Serialized(blkItems)) { if (isCompressed) { using (MemoryStream st = FileOperator.Compressed(ms)) { AppendBlock(toStream, st.ToArray()); } } else { AppendBlock(toStream, ms.ToArray()); } } i++; } return(i); }
/// <summary> /// 将一个xml记录反序列化为指定类型的对象 /// </summary> /// <param name="xml"></param> /// <param name="type"></param> /// <returns></returns> public static object Deserialized(string xml, Type type) { XmlObjectSerializer serializer = ObjectEx.GetXmlObjectSerializer(type); return(Deserialized(xml, serializer)); }
/// <summary> /// 返回一个对象的XML序列化字符串 /// </summary> /// <param name="source"></param> /// <returns></returns> public static string SerializeAsXml(object source) { XmlObjectSerializer serializer = ObjectEx.GetXmlObjectSerializer(source.GetType()); return(SerializeAsXml(source, serializer)); }