Advertisement
andruhovski

Serialize

Sep 11th, 2018
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.69 KB | None | 0 0
  1.  [Serializable]
  2.     class Student : ISerializable
  3.     {
  4.         public string LastName;
  5.         public string FirstName;
  6.         public DateTime birthDate;
  7.         public bool isSelfPayer;
  8.         public double avgScore;
  9.         public Student(SerializationInfo info, StreamingContext context)
  10.         {
  11.             LastName = info.GetString("LastName");
  12.             FirstName = info.GetString("FirstName");
  13.             birthDate = (DateTime)info.GetValue("birthDate", birthDate.GetType());
  14.             isSelfPayer = info.GetBoolean("isSelfPayer");
  15.             avgScore = info.GetDouble("avgScore");
  16.         }
  17.  
  18.         public Student()
  19.         {
  20.             LastName = string.Empty;
  21.             FirstName = string.Empty;
  22.             birthDate = DateTime.Today;
  23.             isSelfPayer = false;
  24.             avgScore = 0.0;
  25.         }
  26.         public void GetObjectData(SerializationInfo info, StreamingContext context)
  27.         {
  28.             info.AddValue("LastName", LastName);
  29.             info.AddValue("FirstName", FirstName);
  30.             info.AddValue("birthDate", birthDate);
  31.             info.AddValue("isSelfPayer", isSelfPayer);
  32.             info.AddValue("avgScore", avgScore);
  33.         }
  34.     }
  35.     class Program
  36.     {
  37.         [STAThread]
  38.         static void Main(string[] args)
  39.         {
  40.             var student = new Student();
  41.             student.FirstName = "Антон";
  42.             student.LastName = "Антонов";
  43.             student.avgScore = 3.4;
  44.             IFormatter formatter = new SoapFormatter();
  45.             FileStream buffer = File.Create("D:\\ABC\\student.xml");
  46.             formatter.Serialize(buffer, student);
  47.             buffer.Close();
  48.         }
  49.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement