序列化与反序列化

建立Man.cs

 

using System.Xml.Serialization;

 

///<summary>

///Man 的摘要说明

///</summary>

 

  [Serializable]

 

    public class Man

    {

 

    

 

         public Man() {

        

        

         }

       [XmlElement("Man_Age")]

         public int Age

         {

             get;

             set;

 

         }

 

         public string Name

         {

             get;

             set;

         }

 

         public string tel

         {

             get;

             set;

         }

        public Man(int Age, string Name ,string tel)

        {

 

            this.Age = Age;

            this.Name = Name;

            this.tel = tel;

 

        }

        public string Show()

        {

 

          return String.Format(" Name : {0}  Age: {1}  tel  :{2}", this.Name, this.Age,this.tel);

        }

        public void AddData(int Age, string Name, string tel)

        {

            this.Age = Age;

            this.Name = Name;

            this.tel = tel;

 

        }

    }

 

 

 

 

1.二进制格式序列化 

  首先引入

using System.Runtime.Serialization.Formatters.Binary;

 

然后通过

 

 

        string Path = "C:\\man.text";

 

        Man m=new Man (18,"小王1","18618618618");

 

        FileStream fs = new FileStream(Path, FileMode.Create);

        BinaryFormatter formatter = new BinaryFormatter();

        formatter.Serialize(fs,m);

        fs.Close();

 

二进制序列化反序列化

 

  

  

        string Path = "C:\\man.text";

        Man m = null;

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

        BinaryFormatter formatter = new BinaryFormatter();

        m = formatter.Deserialize(fs) as Man ;//

        fs.Close();

        return  m.Show();

 

 

 

2.XML序列化

    

 

   

        string Path = "C:\\man.xml";

 

        Man m = new Man();

        m.AddData(18, "小王333", "18618618618");

 

        using (StreamWriter fs = new StreamWriter (Path))

        {

 

            XmlSerializer xmlSerializer = new XmlSerializer(m.GetType());

        

            xmlSerializer.Serialize(fs, m);

         

         

 

        }

xml反序列化

 

        string Path = "C:\\man.xml";

        Man m = new Man ();

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

        XmlSerializer xmlSerializer = new XmlSerializer(m.GetType());

       

       

        m = xmlSerializer.Deserialize(fs) as Man ;//

        fs.Close();

        return  m.Show();

 

 

 

 

   

 

 

 

 

 

插曲:  可以通过       [NonSerialized]  对对象中的的部分数据取消序列化 做到部分暴露

       如果声明一个类 而没有共有字段 那么这个类进行xml序列化的时候  将无法把数据写入xml文件

 

public class XMLserializeHelp
    {

        public static void Serialize<T>(string path, T t) where T:class
        {
            using (StreamWriter sw = new StreamWriter(path))
            {
                XmlSerializer xml = new XmlSerializer(t.GetType());
                xml.Serialize(sw, t);
            }
        }

        public static T Deserialize<T>(string path) where T:class
        {
            T t = default(T);
            FileStream fs = new FileStream(path, FileMode.Open);
            XmlSerializer xmlSerializer = new XmlSerializer(t.GetType());
            t = xmlSerializer.Deserialize(fs) as T;
            fs.Close();
            return t;
        }
    }

 

 

你可能感兴趣的:(序列化与反序列化)