.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Imran Ghani
Sohaib Khalid

Home >> Forum >> C# >> Post New QuestionBookmark and Share Subscribe to Forum

How to serialize a non-serializable class in C#?

Posted By: Jean Paul     Posted Date: September 30, 2010    Points:2   Category :C#
I have to serialize the class EmployeeEntity which is non serializable. I do not have the source of it too.

How can I save the properties of this class to a file and later reload it. Any code will be helpful.

Author: Jean Paul             
Posted Date: October 11, 2010     Points: 5   

I have the answer: Use XmlSerialization

private void Button_Click(object sender, EventArgs e)
Book book = new Book();
book.Title = "Rich Dad Poor Dad";
book.ReleaseDate = new DateTime(2000, 1, 1);
book.Author = new Author() { Name = "Robert Kiyosaki", Email = "rkiyosaki@richdad.com" };

book.AuthorList = new List<Author>();

book.Price = 6.9f;

List<Book> books = new List<Book>() { book, book };

// Serialize

// Deserialize
books = DeserializeFromXML();

public void SerializeToXML(List<Book> movies)
XmlSerializer serializer = new XmlSerializer(typeof(List<Book>));
TextWriter textWriter = new StreamWriter(@"C:\movie.xml");
serializer.Serialize(textWriter, movies);

public List<Book> DeserializeFromXML()
XmlSerializer deserializer = new XmlSerializer(typeof(List<Book>));
TextReader textReader = new StreamReader(@"C:\movie.xml");
List<Book> movies;
movies = (List<Book>)deserializer.Deserialize(textReader);

return movies;

// Classes

public class Book
public string Title
{ get; set; }

public float Price
{ get; set; }

public DateTime ReleaseDate
{ get; set; }

public Author Author { get; set; }

public List<Author> AuthorList { get; set; }

public class Author
public string Name { get; set; }
public string Email { get; set; }

Jean Paul http://jeanpaulva.com/

Post Reply

You must Sign In To post reply
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend