C# 序列化

这个人的文章很好

http://www.cnblogs.com/daniel206/archive/2008/01/16/1042123.html

<textarea cols="50" rows="15" name="code" class="c-sharp">class Program { static void Main(string[] args) { List&lt;XuXu&gt; list = new List&lt;XuXu&gt;(); list.Add(new XuXu(){ID=1,Name=&quot;xuxu&quot;}); list.Add(new XuXu(){ID=2,Name=&quot;bubu&quot;}); string result = SimpleSerializer.Serialize&lt;List&lt;XuXu&gt;&gt;(list); List&lt;XuXu&gt; newList = SimpleSerializer.Deserialize(typeof(List&lt;XuXu&gt;), result) as List&lt;XuXu&gt;; //Stream steam = File.Open(&quot;c://temp3.dat&quot;, FileMode.Create); //BinaryFormatter bf = new BinaryFormatter(); //bf.Serialize(steam, list); //steam.Close(); //Stream steam2 = File.Open(&quot;c://temp3.dat&quot;, FileMode.Open); //BinaryFormatter bf2 = new BinaryFormatter(); //List&lt;XuXu&gt; list_de = (List&lt;XuXu&gt;)bf2.Deserialize(steam2); //Console.Write(list_de[1].Name); } } [Serializable] public class XuXu { public int ID { get; set; } public string Name { get; set; } } public class SimpleSerializer { public static string Serialize&lt;T&gt;(T t) { using (StringWriter sw = new StringWriter()) { XmlSerializer xz = new XmlSerializer(t.GetType()); xz.Serialize(sw, t); return sw.ToString(); } } public static object Deserialize(Type type, string s) { using (StringReader sr = new StringReader(s)) { XmlSerializer xz = new XmlSerializer(type); return xz.Deserialize(sr); } } }</textarea>

你可能感兴趣的:(C# 序列化)