XML序列化封装

简单使用泛型封装下xml序列化

可以放到公共函数库

 

public class XmlSerialHelper

{

    /// <summary>

    /// 反序列化对象

    /// </summary>

    /// <typeparam name="T"></typeparam>

    /// <param name="path"></param>

    /// <returns></returns>

    public static T Deserialize<T>(string path)

    {

        XmlSerializer ser = new XmlSerializer(typeof(T));

        FileStream fs = new FileStream(path, FileMode.Open);

        T t = (T)ser.Deserialize(fs);

        fs.Close();

        return t;

    }

    /// <summary>

    /// 序列化对象

    /// </summary>

    /// <typeparam name="T">对象类型</typeparam>

    /// <param name="path">保存路径</param>

    /// <param name="t">对象实体</param>

    public static void Serialize<T>(string path, T t)

    {

        XmlSerializer ser = new XmlSerializer(typeof(T));

        FileStream fs = new FileStream(path, FileMode.OpenOrCreate);

        ser.Serialize(fs, t);

        fs.Close();

    }

}

 

posted @ 2009-06-27 22:28  zeus2  Views(341)  Comments(1Edit  收藏  举报