码迷,mamicode.com
首页 > Windows程序 > 详细

c#的序列化与反序列化

时间:2015-07-31 08:59:29      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:

反序列化(path为xml文件路径)

 1  public static List<T> GetXmlData(string path)
 2         {
 3             if (File.Exists(path))
 4             {
 5                 XmlSerializer xml = new XmlSerializer(typeof(List<T>));
 6                 using (StreamReader w = new StreamReader(path))
 7                 {
 8                     List<T> list = xml.Deserialize(w) as List<T>;
 9                     return list;
10                 }
11             }
12             return new List<T>();
13         }

序列化

 1  public static void Serialize(List<T> list,string path)
 2         {
 3             XmlSerializer lizer = new XmlSerializer(typeof(List<T>));
 4             
 5             using (System.IO.StreamWriter writer = new System.IO.StreamWriter(path))
 6             {
 7                 System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(List<T>));
 8                 xs.Serialize(writer, list);
 9                 writer.Close();
10             }
11         }

 

c#的序列化与反序列化

标签:

原文地址:http://www.cnblogs.com/jsyFoi/p/4691132.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!