返回顶部

收藏

C#的序列化与反序列化

更多

反序列化(path为xml文件路径)```csharp 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文件存在,则被覆盖 
```csharp
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

标签:c#

收藏

0人收藏

支持

0

反对

0

发表评论