C#的序列化与反序列化

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

反序列化(path为xml文件路径)
public static List<T> GetXmlData(string path)
        {
            if (File.Exists(path))
            {
                XmlSerializer xml = new XmlSerializer(typeof(List<T>));
                using (StreamReader w = new StreamReader(path))
                {
                    List<T> list = xml.Deserialize(w) as List<T>;
                    return list;
                }
            }
            return new List<T>();
        }

序列化,如果xml文件存在,则被覆盖

public static void Serialize(List<T> list,string path)
        {
            XmlSerializer lizer = new XmlSerializer(typeof(List<T>));
            
            using (System.IO.StreamWriter writer = new System.IO.StreamWriter(path))
            {
                System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(List<T>));
                xs.Serialize(writer, list);
                writer.Close();
            }
        }

来自:http://www.cnblogs.com/jsyFoi/p/4691132.html