C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » Serialization »

Load / Save your objects with xml-Serialization


Posted Date: 08 Jun 2009    Resource Type: Code Snippets    Category: Serialization
Author: Farhan Uddin KhanMember Level: Silver    
Rating: 1 out of 5Points: 10



This code snippet shows you how to save / load an object using xml-serialization. It also shows you how to control you serialzation using attributes.
To make this work I've made a small console based application. Please do read the comments throught the code.




using System;
using System.Xml.Serialization;
using System.IO;

namespace ConsoleTestApp
{
[Serializable]
[XmlRoot("EmployeeData")] // EmployeeData will the root node name.
public class Employee
{
[XmlAttribute] // Saves the field as an attribute in the xml file
public int EmployeeID;
public string Name;
public string NationalId;
public decimal BasicSalary;
public decimal HousingAllownce;
public decimal Medical;
[XmlIgnore] // It ignores the field / property from being saved into XML file. As its an auto calculated field.
public decimal TotalSalary
{
get { return (BasicSalary + HousingAllownce + Medical); }
}

// For xml serialization default constructor is mandatory.
public Employee() { }

public Employee(int employeeId, string name, string nationalId, decimal basicSalary, decimal housingAllownce, decimal medical)
{
EmployeeID = employeeId;
Name = name;
NationalId = nationalId;
BasicSalary = basicSalary;
HousingAllownce = housingAllownce;
Medical = medical;
}

///
/// This function serializes the object in an XML file.
///

///
public static bool SerializeEmployee(Employee e, string path)
{
bool Flag = false;
try
{
XmlSerializer Serializer = new XmlSerializer(typeof(Employee));
FileStream Fs = new FileStream(path, FileMode.Create);
Serializer.Serialize(Fs, e);
Flag = true;
Fs.Dispose();
}
catch { }
return Flag;
}

///
/// This function deserializes the object from the XML file.
///

/// XML file path of serialized object.
public static Employee DeSerializeEmployee(string path)
{
Employee ObjEmployee = null;
try
{
FileStream Fs = new FileStream(path, FileMode.Open, FileAccess.Read);
XmlSerializer DeSerializer = new XmlSerializer(typeof(Employee));
ObjEmployee = (Employee)DeSerializer.Deserialize(Fs);
Fs.Dispose();
}
catch {}
return ObjEmployee;
}
}

class Program
{
static void Main(string[] args)
{
Employee ObjEmp_1 = new Employee(101, "Farhan Khan", "10176331078", 9935, 2225, 1725);
bool Flag = Employee.SerializeEmployee(ObjEmp_1, @"C:\Emp.xml");

if (Flag)
{
Employee ObjEmp_2 = Employee.DeSerializeEmployee(@"C:\Emp.xml");
}
}
}
}



Responses


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

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
XML Serialization  .  

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: Object serialization
Previous Resource: Soap Formatter
Return to Discussion Resource Index
Post New Resource
Category: Serialization


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use