标签:
近期想学着封装点类库以便以后去使用,想记录下自己的封装的过程。
using System.IO; using System.Text; using System.Xml.Serialization;
我使用的是拓展方法,第一个参数是拓展的类型,第二个是编码格式
public static string XmlSerialize<T>(this T obj, Encoding encode = null) { encode = encode ?? Encoding.UTF8; //编码格式如果为空,默认为UTF-8编码 string xmlString = string.Empty; //返回的xml字符串 XmlSerializer serializa = new XmlSerializer(typeof(T)); using (MemoryStream ms = new MemoryStream()) { serializa.Serialize(ms, obj); //把要序列化的对象写入到流中 xmlString = encode.GetString(ms.ToArray()); //把流转换成字符串 } return xmlString; }
通过XmlSerializer类的Serialize方法,把对象写入的流中,然后把流转换为字符串返回即可。
/// <summary> /// 对象ToXml文件 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="obj"></param> /// <param name="filePath"></param> /// <param name="encode"></param> /// <returns></returns> public static bool XmlFileSerialize<T>(this T obj, string filePath, Encoding encode = null) { try { encode = encode ?? Encoding.UTF8; //设置默认编码 XmlSerializer serializer = new XmlSerializer(typeof(T)); using (StreamWriter writer = new StreamWriter(filePath, false,encode)) //实例化流对象 { serializer.Serialize(writer, obj); //序列化并写入到文件中 } return true; } catch (Exception ex) { throw new Exception(ex.Message); } }
通过StreamWriter对象,写入到文件。
/// <summary> /// xml字符串To对象 /// </summary> /// <typeparam name="T">实体</typeparam> /// <param name="xmlString">需要反序列化的xml字符串</param> /// <param name="encode">编码</param> /// <returns></returns> public static T XmlDeserialize<T>(this string xmlString, Encoding encode = null) { encode = encode ?? Encoding.UTF8; //设置默认编码 T result = default(T); //为序列化对象赋默认值 XmlSerializer serializa = new XmlSerializer(typeof(T)); using (Stream stream = new MemoryStream(encode.GetBytes(xmlString))) { object obj = serializa.Deserialize(stream); //反序列化xml result = (T)obj; } return result; }
Deserialize方法可以把流反序列化成object对象,签字转换为我们要使用的对象返回即可。
/// <summary> /// Xml文件To对象 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="filePath"></param> /// <param name="encode"></param> /// <returns></returns> public static T XmlFileDeserialize<T>(this string filePath, Encoding encode = null) { encode = encode ?? Encoding.UTF8; T result = default(T); XmlSerializer serializer = new XmlSerializer(typeof(T)); using (StreamReader reader = new StreamReader(filePath, encode)) { result = (T)serializer.Deserialize(reader); } return result; }
使用StreamReader读取指定路径的xml文件,然后通过Deserialize方法把反序列化xml返回object对象,使用强制转换为指定对象。
/// <summary> /// 对象Tojson字符串 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="obj"></param> /// <param name="encode"></param> /// <returns></returns> public static string JsonSerialize<T>(this T obj, Encoding encode = null) { encode = encode ?? Encoding.UTF8; string result = string.Empty; DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T)); using(MemoryStream stream = new MemoryStream()){ serializer.WriteObject(stream, obj); result = encode.GetString(stream.ToArray()); } return result; }
/// <summary> /// json字符串To对象 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="jsonString"></param> /// <param name="encode"></param> /// <returns></returns> public static T JsonDeserialize<T>(this string jsonString, Encoding encode = null) { encode = encode ?? Encoding.UTF8; T result = default(T); DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T)); using(Stream reader = new MemoryStream(encode.GetBytes(jsonString))){ result = (T)serializer.ReadObject(reader); } return result; }
标签:
原文地址:http://www.cnblogs.com/oucuicui/p/4511230.html