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

C#--序列化与反序列化

时间:2015-05-17 15:14:20      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:

引用:

序列化与反序列化

解释:

序列化,就是将一个对象保存到文件中去,而反序列化就是将该文件重新保存为一个对象.

序列化的三种方式:

1.BinaryFormatter

    class BinarySerialize : ISerialize
    {
        FileStream fs;
        string filepath = @"D:/1/1.txt";

        public void Serialize(Book book)
        {
            using (fs = new FileStream(filepath, FileMode.Create))
            {
                BinaryFormatter formate = new BinaryFormatter();
                formate.Serialize(fs, book);
            }
        }

        public Book DeSerialize()
        {
            Book book;
            using (fs = new FileStream(filepath, FileMode.Open))
            {
                BinaryFormatter formate = new BinaryFormatter();
                book = (Book)formate.Deserialize(fs);
            }

            return book;
        }
    }
序列化之后结果如下:
技术分享 

2.SoapFormatter

    class SoapSerialize : ISerialize
    {
        FileStream fs;
        string filepath = @"D:/1/2.txt";

        public void Serialize(Book book)
        {
            using (fs = new FileStream(filepath, FileMode.Create))
            {
                SoapFormatter formate = new SoapFormatter();
                formate.Serialize(fs, book);
            }
        }

        public Book DeSerialize()
        {
            Book book;
            using (fs = new FileStream(filepath, FileMode.Open))
            {
                SoapFormatter formate = new SoapFormatter();
                book = (Book)formate.Deserialize(fs);
            }
            return book;
        }

    }
序列化之后,结果如下;
技术分享 

3.XmlSerializer

    class XMLSeria : ISerialize
    {
        FileStream fs;
        string filepath = @"D:/1/3.txt";

        public void Serialize(Book book)
        {
            using (fs = new FileStream(filepath, FileMode.Create))
            {
                XmlSerializer formate = new XmlSerializer(typeof(Book));
                formate.Serialize(fs, book);
            }
        }

        public Book DeSerialize()
        {
            Book book;
            using (fs = new FileStream(filepath, FileMode.Open))
            {
                XmlSerializer formate = new XmlSerializer(typeof(Book));
                book = (Book)formate.Deserialize(fs);
            }
            return book;
        }

    }
序列化之后,结果如下:
技术分享 
完整源码如下:
代码段1:
定义类库Define
namespace Define
{
    [Serializable]
    public class Book
    {
        string bookname;

        public string BookName
        {
            get
            {
                return this.bookname;
            }
            set
            {
                this.bookname = value;
            }
        }

        int price;
        public int Price
        {
            get
            {
                return this.price;
            }
            set
            {
                this.price = value;
            }
        }

        public override string ToString()
        {
            return "BookName:" + this.bookname + "<br/>Price:" + this.price.ToString() + "<br/>";
        }
    }


    public interface ISerialize
    {
        void Serialize(Book book);
        Book DeSerialize();
    }

    class BinarySerialize : ISerialize
    {
        FileStream fs;
        string filepath = @"D:/1/1.txt";

        public void Serialize(Book book)
        {
            using (fs = new FileStream(filepath, FileMode.Create))
            {
                BinaryFormatter formate = new BinaryFormatter();
                formate.Serialize(fs, book);
            }
        }

        public Book DeSerialize()
        {
            Book book;
            using (fs = new FileStream(filepath, FileMode.Open))
            {
                BinaryFormatter formate = new BinaryFormatter();
                book = (Book)formate.Deserialize(fs);
            }

            return book;
        }
    }

    class SoapSerialize : ISerialize
    {
        FileStream fs;
        string filepath = @"D:/1/2.txt";

        public void Serialize(Book book)
        {
            using (fs = new FileStream(filepath, FileMode.Create))
            {
                SoapFormatter formate = new SoapFormatter();
                formate.Serialize(fs, book);
            }
        }

        public Book DeSerialize()
        {
            Book book;
            using (fs = new FileStream(filepath, FileMode.Open))
            {
                SoapFormatter formate = new SoapFormatter();
                book = (Book)formate.Deserialize(fs);
            }
            return book;
        }

    }


    class XMLSeria : ISerialize
    {
        FileStream fs;
        string filepath = @"D:/1/3.txt";

        public void Serialize(Book book)
        {
            using (fs = new FileStream(filepath, FileMode.Create))
            {
                XmlSerializer formate = new XmlSerializer(typeof(Book));
                formate.Serialize(fs, book);
            }
        }

        public Book DeSerialize()
        {
            Book book;
            using (fs = new FileStream(filepath, FileMode.Open))
            {
                XmlSerializer formate = new XmlSerializer(typeof(Book));
                book = (Book)formate.Deserialize(fs);
            }
            return book;
        }

    }

    public interface BaseFacotry
    {
        ISerialize CreateSerialize();
    }

    class BinaryFactory : BaseFacotry
    {
        public ISerialize CreateSerialize()
        {
            return new BinarySerialize();
        }
    }

    class SoapFactory : BaseFacotry
    {
        public ISerialize CreateSerialize()
        {
            return new SoapSerialize();
        }
    }

    class XMLFactory : BaseFacotry
    {
        public ISerialize CreateSerialize()
        {
            return new XMLSeria();
        }
    }
}
代码段2:
前台调用:
    <asp:Button ID="BinaryFactory" runat="server" Text="BinarySerialize" onclick="Btn_Click" />
    <asp:Button ID="SoapFactory" runat="server" Text="SoapSerialize" onclick="Btn_Click" />
    <asp:Button ID="XMLFactory" runat="server" Text="XMLSerialize" onclick="Btn_Click"  />

    protected void Btn_Click(object sender, EventArgs e)
    {
        Book book = new Book();
        book.BookName = "入门经典";
        book.Price = 120;

        string id = ((Button)sender).ID;

        BaseFacotry factory = (BaseFacotry)Assembly.Load("Define").CreateInstance("Define." + id);

        ISerialize serialize = factory.CreateSerialize();
        serialize.Serialize(book);

        Book b = serialize.DeSerialize();
        Response.Write(b.ToString());
    }

C#--序列化与反序列化

标签:

原文地址:http://www.cnblogs.com/loveYN/p/4509703.html

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