.NET 序列化

 一:BinaryFomatter  VS.  SoapFomatter

 

  class  Program
    
{
        
static void Main(string[] args)
        
{
            
//UseBinaryFormatter();

            UseSoapFormatter();

            Console.ReadLine();
        }


        
/*
         * 使用BinaryFormatter和SoapFormatter序列化类时,必须要标记[Serializable]属性。
         * 不需要有不带任何参数的构造函数也可以反序列化。
         * 并且可以序列化所有的变量和属性,而不仅限于公有的成员。
         * 对于不需要序列化的成员,可以使用[NonSerialized]属性进行标记。
         
*/


        
// 使用BinaryFormatter类
        public static void UseBinaryFormatter()
        
{
            Console.WriteLine(
"---use BinaryFormatter---");

            Employee emp 
= new Employee("Daniel"100"Guangzhou", DateTime.Now);
            Console.WriteLine(
"before serialize  {0} ", emp.ToString());

            BinaryFormatter formatter 
= new BinaryFormatter();
            FileStream tofile 
= new FileStream(@"C:Employee.bin", FileMode.Create);
            formatter.Serialize(tofile, emp);
            tofile.Close();

            FileStream fromfile 
= new FileStream(@"C:Employee.bin", FileMode.Open);
            Employee emp1 
= (Employee)formatter.Deserialize(fromfile);
            fromfile.Close();

            Console.WriteLine(
"after serialzie  {0} ", emp1.ToString());
        }



        
// 使用SoapFormatter类
        public static void UseSoapFormatter()
        
{
            Console.WriteLine(
"---use SoapFormatter---");

            Employee emp 
= new Employee("Daniel"100"Guangzhou", DateTime.Now);
            Console.WriteLine(
"before serialize  {0} ", emp.ToString());

            SoapFormatter formatter 
= new SoapFormatter();
            FileStream tofile 
= new FileStream(@"C:Employee.soap", FileMode.Create);
            formatter.Serialize(tofile, emp);
            tofile.Close();

            FileStream fromfile 
= new FileStream(@"C:Employee.soap", FileMode.Open);
            Employee emp1 
= (Employee)formatter.Deserialize(fromfile);
            fromfile.Close();

            Console.WriteLine(
"after serialzie  {0} ", emp1.ToString());
        }

    }


    [Serializable]
    
public   class  Employee
    
{
        
public string Name;
        
public int Age;     // 公有变量,但不初始化,也会被序列化
        [NonSerialized]
        
private int Salary;
        
protected string Address;
        
public DateTime EnrollmentDate;

        
public Employee(string name, int salary, string address, DateTime enrollmentDate)
        
{
            
this.Name = name;
            
this.Salary = salary;
            
this.Address = address;
            
this.EnrollmentDate = enrollmentDate;
        }


        
public override string ToString()
        
{
            
return string.Format("Name:{0}, Age:{1}, Salary:{2}, Address:{3}"this.Name, this.Age, this.Salary, this.Address);
        }

    }


二  XMLFomatter

 

class  Program
    
{
        
static void Main(string[] args)
        
{
            Serialize();

            Deserialize();

            Console.ReadLine();
        }


        
/*
         * 使用XmlSerializer序列化类时,该类不需要必须要标记[Serializable]属性。
         * 需要有不带任何参数的构造函数。
         * 并且只可以序列化公有的成员,私有成员和保护成员在序列化中会丢失。
         
*/


        
public static void Serialize()
        
{
            Console.WriteLine(
"---Serialize---");

            Employee emp 
= new Employee("Daniel"100"Guangzhou"new string[] "020-82319259""13570479050""020-12345678"});
            Console.WriteLine(emp.ToString());

            FileStream stream 
= new FileStream(@"C:Employee.xml", FileMode.Create);
            XmlSerializer serializer 
= new XmlSerializer(typeof(Employee));

            serializer.Serialize(stream, emp);
            stream.Close();
        }


        
public static void Deserialize()
        
{
            Console.WriteLine(
"---Deserialize---");

            FileStream stream 
= new FileStream(@"C:Employee.xml", FileMode.Open);
            XmlSerializer serializer 
= new XmlSerializer(typeof(Employee));

            Employee emp 
= (Employee)serializer.Deserialize(stream);
            stream.Close();

            Console.WriteLine(emp.ToString());
        }

    }


    
public   class  Employee
    
{
        [XmlAttribute]
        
public string Name;
        
public int Age;     // 公有变量,但不初始化,也会被序列化
        private int Salary;
        
protected string Address;   
       
        [XmlArray(ElementName
="Telephones")]
        [XmlArrayItem(ElementName
="Telephone")]
       
        
public string[] Telephone;

        
// 为了序列化,所以必须要有不带任何参数的构造函数
        public Employee()
        
{
        }


        
public Employee(string name, int salary, string address, string[] telephone)
        
{
            
this.Name = name;
            
this.Salary = salary;
            
this.Address = address;
            
this.Telephone = telephone;
        }


        
public override string ToString()
        
{
            
return string.Format("Name:{0}, Age:{1}, Salary:{2}, Address:{3}"this.Name, this.Age, this.Salary, this.Address);
        }

    }


三 自定义Fomatter

 

class  Program
    
{
        
static void Main(string[] args)
        
{
            Serialize();

            Deserialize();

            Console.ReadLine();

        }


        
/*
         * 使用自定义序列化类时,必须要标记[Serializable]属性,并且要实现ISerializable接口。
         
*/


        
public static void Serialize()
        
{
            Employee emp 
= new Employee("Daniel"100"Guangzhou");
            Console.WriteLine(
"before serialize  {0} ", emp.ToString());

            IFormatter formatter 
= new SoapFormatter();
            FileStream stream 
= new FileStream(@"C:Employee.soap", FileMode.Create);
            formatter.Serialize(stream, emp);
            stream.Close();
        }


        
public static void Deserialize()
        
{
            IFormatter formatter 
= new SoapFormatter();
            Stream stream 
= new FileStream(@"C:Employee.soap", FileMode.Open);
            Employee emp 
= (Employee)formatter.Deserialize(stream);
            stream.Close();

            Console.WriteLine(
"after serialize  {0} ", emp.ToString());
        }

    }


    [Serializable]
    
public   class  Employee : ISerializable
    
{
        
public string Name;
        
public int Age;     // 公有变量,但不初始化,也会被序列化
        private int Salary;
        
protected string Address;

        
public Employee(string name, int salary, string address)
        
{
            
this.Name = name;
            
this.Salary = salary;
            
this.Address = address;
        }


        
protected Employee(SerializationInfo info, StreamingContext context)
        
{
            
this.Name = "FromFile:" + info.GetString("Name")
            
this.Salary = info.GetInt32("Salary");
            
this.Address = "FromFile:" + info.GetString("Address");
        }


        
public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
        
{
            info.AddValue(
"Name""ToFile:" + this.Name);
            info.AddValue(
"Salary"this.Salary);
            info.AddValue(
"Address""ToFile:" + this.Address);
        }


        
public override string ToString()
        
{
            
return string.Format("Name={0}, Age={1}, Salary={2}, Address={3}"this.Name, this.Age, this.Salary, this.Address);
        }

    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值