Exemplo n.º 1
0
        static void Main(string[] args)
        {
            //Console.WriteLine("---------------Binary Serialization--------------");
            //Student student = new Student(2,"abc",60);
            //Console.WriteLine("Before Serialization \n {0}", student);
            //Student.BinarySerialization("iCalibrator.dat",student);
            //Student deSerializedStudent = Student.BinaryDeserialization("iCalibrator.dat");
            //Console.WriteLine("After Serialization \n {0}",deSerializedStudent);

            Console.WriteLine("---------------XML Serialization-----------------");
            Student student1 = new Student(4, "Sarab", 70);
            Console.WriteLine("Before Serialization \n {0}", student1);
            Student.XmlSerialization("iCalibrator.xml", student1);
            Student student2 = Student.XmlDeserialization("iCalibrator.xml");
            Console.WriteLine("After Serialization:{0}", student2);

            Console.WriteLine("---------------Soap Serialization-----------------");
            Student student3 = new Student(5, "bad", 80);
            Console.WriteLine("Before Serialization \n {0}", student3);
            Student.SoapSerialization("iCalibrator.soap", student3);
            Student student4 = Student.SoapDeserialization("iCalibrator.soap");
            Console.WriteLine("After Serialization \n {0}", student4);
            Console.ReadKey();
        }
Exemplo n.º 2
0
        public static void XmlSerialization(String fileName, Student student)
        {
            /*  XmlSerializer serializer = new XmlSerializer(typeof(Student));
            StreamWriter stream = new StreamWriter(fileName);
            try
            {
                serializer.Serialize(stream, student);
            }
            catch (SerializationException exception)
            {
                Console.WriteLine("Serialization Exception:" + exception.Message);
            }
            finally
            {
                stream.Close();

            }*/

            DataContractSerializer serializer = new DataContractSerializer(typeof(Student));
            FileStream file = new FileStream(fileName, FileMode.Create);
            try
            {
                using (XmlWriter writer = XmlWriter.Create(file))
                {
                    serializer.WriteObject(writer, student);
                }
                file.Close();
            }
            catch (Exception exception)
            {

                Console.WriteLine("XML Serilaization:"+exception.Message);
            }
        }
Exemplo n.º 3
0
 public static void SoapSerialization(String fileName, Student student)
 {
     try
     {
         Stream stream = File.Open(fileName, FileMode.Create);
         SoapFormatter soapFormatter = new SoapFormatter();
         soapFormatter.Serialize(stream, student);
         stream.Close();
     }
     catch (Exception exception)
     {
         Console.WriteLine("Exception While writng :{0}", exception.Message);
     }
 }