4 Replies - 4806 Views - Last Post: 01 February 2011 - 11:29 AM Rate Topic: -----

#1 Elliotd123  Icon User is offline

  • New D.I.C Head

Reputation: 12
  • View blog
  • Posts: 37
  • Joined: 24-January 11

Serialization in C#

Posted 31 January 2011 - 07:47 PM

Are there any simple serialization classes for C# that I can use to make it simple to save an object to a file? I should think it CAN be as simple as a method that takes the object and the file name as arguments. Or maybe I'm just way off.
Is This A Good Question/Topic? 0
  • +

Replies To: Serialization in C#

#2 Ticon  Icon User is offline

  • D.I.C Regular

Reputation: 25
  • View blog
  • Posts: 307
  • Joined: 20-August 09

Re: Serialization in C#

Posted 31 January 2011 - 07:55 PM

In my experience with it I would say your way off unfortunately. Check out the tutorial by SixofEleven titled Serializing an object with XML and SOAP I hated doing this as it was difficult but once i got done with it, I felt great because I Pulled through the journey :)
Was This Post Helpful? 0
  • +
  • -

#3 CodingSup3rnatur@l-360  Icon User is online

  • D.I.C Addict
  • member icon

Reputation: 916
  • View blog
  • Posts: 921
  • Joined: 30-September 10

Re: Serialization in C#

Posted 01 February 2011 - 02:23 AM

I don't think you are particuarly far off actually. Here is a brief example of how simple serialization can be. In this example, I am going to serialize a list of Person objects (in a console application).

My Person class

[Serializable] //don't even need this in this example as I have explained in bold below
public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }



Add these using statements to the Program.cs file (or wherever you are doing the serialization from):

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



In this example, I am going to create a list of two Person objects in the Main method of the console application, as so:

//initialise list with two people
List<Person> people = new List<Person>() { new Person() { Name = "Jason", Age = 18 }, new Person() { Name = "John", Age = 33 } };


Now, the only thing left to do is to create our method that will serialize the list of people (still in Program class):


        private static void Serialize(object objectToSerialize, string filePath)
        {
            //create a new file stream
            using (Stream fStream = new FileStream(filePath,
                FileMode.Create, FileAccess.Write, FileShare.None))
            {
                //create an XmlSerializer passing in the type of the object we want to serialize
                XmlSerializer xmlFormat = new XmlSerializer(typeof(List<Person>));
                //call serialize on the Serializer
                xmlFormat.Serialize(fStream, objectToSerialize);
            }
        }




Add that's it! All we have to do now is call the method to Serialize the objects.

Note also that we don't even have to mark the Person class with the Serializable attribute as the List<T> class is already marked as Serializable. However, it's probably safer to use the Serializable attribute ([Serializable]) on the Person class, to just cover the possibility that we may want to serialize single Person objects on their own without the list collection in the future :)

Our call to Serialize looks like this:

 static void Main(string[] args)
        {
            //our list of people from earlier
            List<Person> people = new List<Person>() { new Person() { Name = "Jason", Age = 18 }, new Person() { Name = "John", Age = 33 } };[/

            //call our serialize method passing in our object to serialize and our filepath
            Serialize(people, "People.xml");
        }



The .xml file we get out looks like this:

Quote

<?xml version="1.0"?>
<ArrayOfPerson xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Person>
<Name>Jason</Name>
<Age>18</Age>
</Person>
<Person>
<Name>John</Name>
<Age>33</Age>
</Person>
</ArrayOfPerson>


In this example, we used the XmlSerializer object to output a standard .xml file. You can also output the objects in binary, SOAP or even JSON format using very similar methods, just different formatter objects!

This post has been edited by CodingSup3rnatur@l-360: 01 February 2011 - 02:44 AM

Was This Post Helpful? 4
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Closing in on 5,000
  • member icon

Reputation: 4924
  • View blog
  • Posts: 10,454
  • Joined: 02-June 10

Re: Serialization in C#

Posted 01 February 2011 - 07:29 AM

But you do have to realize that certain thing are inherently NOT serializable.
GUI controls for example. You can't serialize a form or a button for example.

This is why it is best to separate data from display.

ClerkObj.cs would be a class with all the properties of a store clerk like name, ID, wages etc.
ClerkDlg.cs would be a form dialog with a ClerkObj property. The dialog exists to show the values of the object class. That way you can serialize the ClerkObj.
Was This Post Helpful? 1
  • +
  • -

#5 Robin19  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 206
  • View blog
  • Posts: 436
  • Joined: 07-July 10

Re: Serialization in C#

Posted 01 February 2011 - 11:29 AM

It would be great if someone wrote a tutorial about this.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1