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.
Serialization in C#
Page 1 of 14 Replies - 4806 Views - Last Post: 01 February 2011 - 11:29 AM
Replies To: Serialization in C#
#2
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
#3
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
Add these using statements to the Program.cs file (or wherever you are doing the serialization from):
In this example, I am going to create a list of two Person objects in the Main method of the console application, as so:
Now, the only thing left to do is to create our method that will serialize the list of people (still in Program class):
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:
The .xml file we get out looks like this:
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!
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>
<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
#4
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.
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.
#5
Re: Serialization in C#
Posted 01 February 2011 - 11:29 AM
It would be great if someone wrote a tutorial about this.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote






|