As we know you make an object serializable by either implementing the ISerializable interface or by placing the [Serializable] at the top of the class.
1. Students Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SysDiagnosis
{
public class Student
{
public string Name { get; set; }
public string ID { get; set; }
public int Age { get; set; }
public string Gender { get; set; }
public double Grade { get; set;}
}
}
2. SerilizeHelper Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
namespace SysDiagnosis
{
public class SerializeHelper
{
public static void SerializeMe(object obj, string strPath)
{
XmlSerializer serilizser = new XmlSerializer(obj.GetType());
StreamWriter writter = new StreamWriter(strPath);
serilizser.Serialize(writter.BaseStream,obj);
}
}
}
3. Main Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
namespace SysDiagnosis
{
class Program
{
static void Main(string[] args)
{
Student student = new Student();
student.Name = "Vinodh";
student.ID = "EE01";
student.Age = 27;
student.Gender = "Male";
student.Grade = 7.50;
Console.WriteLine("Serializing object to XML");
SerializeHelper.SerializeMe(student,@"C:\students.xml");
Console.ReadLine();
}
}
}
4.Ouput File : (c:\students.xml)
<?xml version="1.0"?>
<Student xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Name>Vinodh</Name>
<ID>EE01</ID>
<Age>27</Age>
<Gender>Male</Gender>
<Grade>7.5</Grade>
</Student>
class Program
{
static void Main(string[] args)
{
Student student = new Student();
student.Name = "Vinodh";
student.ID = "EE01";
student.Age = 27;
student.Gender = "Male";
student.Grade = 7.50;
Console.WriteLine("Serializing Object to XML");
SerializeHelper.SerializeMe(student, @"C:\students.xml");
Console.WriteLine("Serializing XML to Object");
SerializeHelper.SerializeMe(@"C:\students.xml", new Student());
}
}
namespace SysDiagnosis
{
public class SerializeHelper
{
public static void SerializeMe(object obj, string strPath)
{
XmlSerializer serilizser = new XmlSerializer(obj.GetType());
StreamWriter writter = new StreamWriter(strPath);
serilizser.Serialize(writter.BaseStream,obj);
writter.Close();
}
public static void SerializeMe(string strPath, Student obj)
{
XmlSerializer serilizer = new XmlSerializer(obj.GetType());
StreamReader reader = new StreamReader(strPath);
object deserialize = serilizer.Deserialize(reader.BaseStream);
obj = deserialize as Student;
Console.WriteLine(obj.Name);
Console.WriteLine(obj.ID);
Console.WriteLine(obj.Age);
Console.WriteLine(obj.Gender);
Console.WriteLine(obj.Grade);
}
}
}
Serializatiion :
https://www.codeproject.com/Articles/1789/Object-Serialization-using-C
http://www.c-sharpcorner.com/uploadfile/yougerthen/how-to-serialize-an-object-using-an-iserializable-interface/
No comments:
Post a Comment