zoukankan      html  css  js  c++  java
  • 序列化和反序列 asp.net c#

    In this article we are going to see how to serialize and deserialize an object as binary data using the binary formatter.
    Step 1: Used Namespace
    using System; using System.Collections; using System.IO; using System.Runtime.Serialization.Formatters.Binary;

    Step 2: Usage

    protected void Page_Load(object sender, EventArgs e) {     Employees emps = new Employees();

        emps.Add(new Employee("1", "Lajapathy"));     emps.Add(new Employee("2", "Anand"));

        emps.Add(new Employee("3", "Sathiya"));     emps.Add(new Employee("4", "Lakshmi"));     emps.Add(new Employee("5", "Parthiban"));       string pth = @"D:Test.bin";       //Serializing the collection     Serialize(emps, pth);       //Deserializing the collection     Deserialize(pth); }

    Step 3: Serializing the Object using Binary Formatter
    //Serializing the List public void Serialize(Employees emps, String filename) {     //Create the stream to add object into it.     System.IO.Stream ms = File.OpenWrite(filename);      //Format the object as Binary

        BinaryFormatter formatter = new BinaryFormatter();     //It serialize the employee object     formatter.Serialize(ms, emps);     ms.Flush();     ms.Close();     ms.Dispose(); }

    Step 4: Deserializing the Object using Binary Formatter
    //Deserializing the List public void Deserialize(String filename) {     //Format the object as Binary     BinaryFormatter formatter = new BinaryFormatter();       //Reading the file from the server     FileStream fs = File.Open(filename, FileMode.Open);       object obj = formatter.Deserialize(fs);     Employees emps = (Employees)obj;     fs.Flush();     fs.Close();     fs.Dispose();      foreach (Employee employee in emps)     {        Response.Write(employee.Name + "<br/>");     } }

    Step 5: Output Path of Serialization:
    Step 6: Output of Deserialization:
     

    //Deserializing the List public void Deserialize(String filename) {     //Format the object as Binary     BinaryFormatter formatter = new BinaryFormatter();       //Reading the file from the server     FileStream fs = File.Open(filename, FileMode.Open);       object obj = formatter.Deserialize(fs);     Employees emps = (Employees)obj;     fs.Flush();     fs.Close();     fs.Dispose();       foreach (Employee employee in emps)     {         Response.Write(employee.Name + "<br/>");     } }
    Copy & Paste Code Snippet
    using System; using System.Collections; using System.IO; using System.Runtime.Serialization.Formatters.Binary;   namespace SampleApplication {     public partial class ObjectSerialization : System.Web.UI.Page     {         protected void Page_Load(object sender, EventArgs e)         {             Employees emps = new Employees();             emps.Add(new Employee("1", "Lajapathy"));             emps.Add(new Employee("2", "Anand"));             emps.Add(new Employee("3", "Sathiya"));

                emps.Add(new Employee("4", "Lakshmi"));             emps.Add(new Employee("5", "Parthiban"));               string pth = @"D:Test.bin";               //Serializing the collection             Serialize(emps, pth);               //Deserializing the collection             Deserialize(pth);         }          //Serializing the List         public void Serialize(Employees emps, String filename)         {             //Create the stream to add object into it.             System.IO.Stream ms = File.OpenWrite(filename);               //Format the object as Binary             BinaryFormatter formatter = new BinaryFormatter();               //It serialize the employee object             formatter.Serialize(ms, emps);             ms.Flush();             ms.Close();             ms.Dispose();         }          //Deserializing the List         public void Deserialize(String filename)         {             //Format the object as Binary             BinaryFormatter formatter = new BinaryFormatter();               //Reading the file from the server             FileStream fs = File.Open(filename, FileMode.Open);              object obj = formatter.Deserialize(fs);             Employees emps = (Employees)obj;            fs.Flush();             fs.Close();             fs.Dispose();              foreach (Employee employee in emps)             {                 Response.Write(employee.Name + "<br/>");             }         }     }      //Classes     [Serializable]     public class Employee     {          public Employee(String id, String name)         {             _ID = id;             _Name = name;         }          private String _ID = String.Empty;         private String _Name = String.Empty;          public String ID         {             get             {                 return _ID;             set             {                 _ID = value;             }         }         public String Name         {             get             {

                    return _Name;             }             set             {                 _Name = value;             }         }     }      [Serializable]     public class Employees : CollectionBase     {         //Constructor         public Employees()         {          }           //Add function         public void Add(Employee objT)         {             this.List.Add(objT);         }          //Indexer         public Employee this[int i]         {             get             {                 return (Employee)this.List[i];             }             set             {                 this.List.Add(value);             }         }     }}

    Thanks for reading this article(http://www.c-sharpcorner.com/UploadFile/d3e4b1/serializing-and-deserializing-the-object-as-binary-data-usin/). Have a nice day.

  • 相关阅读:
    中国登山队员首次登上地球之巅珠穆朗玛峰的时间与意义及影响 (转)
    兰戈利尔人(斯蒂芬.金)
    冥界系列一:麝月 (作者:钱其强)
    席慕容独白
    【心理寓言】小偷在鸡舍偷了只鸡
    美国恐怖故事第一季事件时间表
    大学生逃课的暴笑理由
    原来他们四个也是有故事的男人
    爆笑:七八十年代各地最流行顺口溜 网友:太经典了
    中国的世界之最
  • 原文地址:https://www.cnblogs.com/happy-Chen/p/3591468.html
Copyright © 2011-2022 走看看