日期:2014-05-16  浏览次数:20434 次

C#序列化字符串,对象,xml,json,集合数组,字典

1.对象和xml的序列化和反序列化:

先随便建一个实体类

 public class Person
    {
        public Person()
        {
        }

        public Person(string name, int age)
        {
            Name = name;
            Age = age;
        }

        public string Name { get; set; }

        public int Age { get; set; }

        public DateTime LastLoginTime { get; set; }

        public List<Course> Courses = new List<Course>();
    }
 public class Course
    {
        public Course()
        {
        }

        public Course(string name, string description)
        {
            Name = name;
            Description = description;
        }

        public string Name
        {
            get;
            set;
        }

        public string Description
        {
            get;
            set;
        }
    }

??对象和xml序列化和反序列化方法

        /// <summary>
        /// 序列化
        /// </summary>
        /// <param name="filePath"></param>
        public static void Serialiaze(String filePath)
        {
            Person c = new Person("James Chen",35);
            c.Courses.Add(new Course("高等数学", "思维工具"));
            c.Courses.Add(new Course("大学英语", "交流工具"));
            c.Courses.Add(new Course("离散数学", "计算机理论"));
            XmlSerializer xs = new XmlSerializer(typeof(Person));
            Stream stream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
            xs.Serialize(stream, c);
            stream.Close();
        }

        /// <summary>
        /// 反序列化
        /// </summary>
        /// <param name="filePath"></param>
        public static void Deserialize(String filePath)
        {
            XmlSerializer xs = new XmlSerializer(typeof(Person));
            Stream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            Person p = (Person)xs.Deserialize(stream);
            stream.Close();
        }

?调用方法

string path = Server.MapPath("upload/test.xml");
Serializer.Serialiaze(path);
Serializer.Deserialize(path);

?

2.字符串和对象的序列化和反序列化

/// <summary>
        /// 序列化 对象到字符串
        /// </summary>
        /// <param name="obj">泛型对象</param>
        /// <returns>序列化后的字符串</returns>
        public static string Serialize<T>(T obj)
        {
            try
            {
                IFormatter formatter = new BinaryFormatter();
                MemoryStream stream = new MemoryStream();
                formatter.Serialize(stream, obj);
                stream.Position = 0;
                byte[] buffer = new byte[stream.Length];
                stream.Read(buffer, 0, buffer.Length);
                stream.Flush();
                stream.Close();
                return Convert.ToBase64String(buffer);
            }
            catch (Exception ex)
            {
                throw new Exception("序列化失败,原因:" + ex.Message);
            }
        }

        /// <summary>
        /// 反序列化 字符串到对象
        /// </summary>
        /// <param name="obj">泛型对象</param>
        /// <param name="str">要转换为对象的字符串</param>
        /// <returns>反序列化出来的对象</returns>
        public static T Desrialize<T>(T obj, string str)
        {
            try
            {
                obj = default(T);
                IFormatter formatter = new BinaryFormatter();
                byte[] buffer = Convert.FromBase64String(str);
                MemoryStream stream = new MemoryStream(buffer);
                obj = (T)formatter.Des