/// <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)); }
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> /// <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> /// 将一个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)); }