C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !






Serialize Objects using XmlSerializer class


Posted Date: 05 Sep 2008    Resource Type: Code Snippets    Category: Serialization
Author: Sabu C AlexMember Level: Gold    
Rating: Points: 10



Xml Serialization
--------------------------------------
Serialization is the process of converting an object to a format that can be transfer through a network or can be save to a location(file or DB).
The serialized data contains the object's informations like Version,Culture,PublicKeyToken,Type etc.
Deserialization is the reverse process of serialization, that is reconstructing the object from the serialized state to its original state.

Here is an eg:

A class "Employee" and its collection class "Employees". I marked these classes as Serializable with the [Serializable] attribute.
and also i am using XmlSerializer class to Serialize and Deserialize this object.

//Form

private void button1_Click
(object sender, EventArgs e)
{
Employees<Employee> emps = new Employees<Employee>();
emps.Add(new Employee("1", "Sabu"));
emps.Add(new Employee("2", "Litson"));

String pth = @"E:\Test.xml";
XmlSerialize(emps, pth);
XmlDeserialize(pth);
}

public void XmlSerialize(Employees<Employee> emps, String filename)
{
System.IO.Stream ms = File.OpenWrite(filename);
System.Xml.Serialization.XmlSerializer xmlSer = new System.Xml.Serialization.XmlSerializer(emps.GetType());
xmlSer.Serialize(ms, emps);
ms.Flush();
ms.Close();
ms.Dispose();
xmlSer = null;
}

public void XmlDeserialize(String filename)
{
System.Xml.Serialization.XmlSerializer xmlSer = new System.Xml.Serialization.XmlSerializer(typeof(Employees<Employee>));
FileStream fs = new FileStream(filename, FileMode.Open);
object obj = xmlSer.Deserialize(fs);
Employees<Employee> emps = (Employees<Employee>)obj;
MessageBox.Show(emps[1].Name);
}


//Classes

[Serializable]
public class Employee
{

public Employee(String id, String name)
{
_ID = id;
_Name = name;
}

private String _ID = String.Empty;
private String _Name = String.Empty;

public String ID
{
get
{
return _ID;
}
set
{
_ID = value;
}
}

public String Name
{
get
{
return _Name;
}
set
{
_Name = value;
}
}
}

[Serializable]
public class Employees<T>:CollectionBase
{
//Constructor
public Employees()
{

}

//Add function
public void Add(T objT)
{
this.List.Add(objT);
}

//Indexer
public T this[int i]
{
get
{
return (T) this.List[i];
}
set
{
this.List.Add(value);
}
}
}





Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Xml Serialization  .  Serialize Objects using XmlSerializer class  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Serialize Objects using BinaryFormatter
Previous Resource: Deserialization of XML to Hashtable
Return to Discussion Resource Index
Post New Resource
Category: Serialization


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use