序列化对象为xml:
/// <summary> /// 序列化对象 /// </summary> /// <typeparam name="T">对象类型</typeparam> /// <param name="t">对象</param> /// <returns></returns> public static string Serialize<T>(T t) { using (StringWriter sw = new StringWriter()) { XmlSerializer xz = new XmlSerializer(t.GetType()); xz.Serialize(sw, t); return sw.ToString(); } }
xml反序列化为对象:
/// <summary> /// 反序列化为对象 /// </summary> /// <param name="type">对象类型</param> /// <param name="s">对象序列化后的Xml字符串</param> /// <returns></returns> public static object Deserialize(Type type, string s) { using (StringReader sr = new StringReader(s)) { XmlSerializer xz = new XmlSerializer(type); return xz.Deserialize(sr); } }
结合【模拟Post请求】http://blog.csdn.net/a497785609/archive/2011/05/21/6437154.aspx可以实现客户端和网站的通讯。