- Make one sEmployee Class with their Property. And one Employeecollection class of that sEmployee Class.
public class sEmployee
{
public string FirstName;
public string LastName;
public int Age;
public sEmployee()
{
}
public sEmployee(string first, string last, int age)
{
this.FirstName = first;
this.LastName = last;
this.Age= age;
}
}
- Implement the CollectionBase for adding multiple employee item in collection by override their Add Method.
public class EmployeeCollection : CollectionBase
{
public EmployeeCollection() { }
public EmployeeCollection(EmployeeCollection value) { this.AddRange(value); }
public EmployeeCollection(sEmployee[] value) { this.AddRange(value); }
public sEmployee this[int index] { get { return ((sEmployee)(List[index])); } set { List[index] = value; } }
public int Add(sEmployee value) { return List.Add(value); }
public void AddRange(sEmployee[] value) { for (int i = 0; i < i =" 0;">
- Now create one sample page for serialization and Deserialization.here i have took one .aspx page and code behind i have wrote this simple code,and saving that data into XML file.
EmployeeCollection empCollection = new EmployeeCollection();
empCollection.Add(new sEmployee("Paresh0", "Patel0", 20));
empCollection.Add(new sEmployee("Paresh1", "Patel1", 21));
empCollection.Add(new sEmployee("Paresh2", "Patel2", 22));
string op = string.Empty;
using (StringWriter writer = new StringWriter(new StringBuilder()))
{
XmlSerializer xs = new XmlSerializer(typeof(EmployeeCollection));
xs.Serialize(writer, empCollection);
op = writer.ToString();
}
File.WriteAllText("c:\\emp.xml", op);
Deserialization :
string ip = string.Empty;
EmployeeCollection collection = new EmployeeCollection();
ip = File.ReadAllText("c:\\emp.xml");
using (StringReader reader = new StringReader(ip))
{
XmlSerializer xs = new XmlSerializer(typeof(EmployeeCollection));
collection = (EmployeeCollection)xs.Deserialize(reader);
}
No comments:
Post a Comment