It is currently Fri Sep 10, 2010 9:20 am
Index  •  FAQ  •  Search

All times are UTC + 6 hours




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: How make Object persistance/Serializable and Deserializable
PostPosted: Mon May 18, 2009 10:07 pm 
User's Group : S.U.S.T

Joined: Thu Mar 12, 2009 9:19 pm
Posts: 10
Quote:
This post is specially for mitu, SUST, cse 02


How to make an Object persistance:
You can make an object serializable. It will save the object and/or its values in a binary or Xml files based on the format you specified.
Tips: Use binary formatter when you are that you serialized object's consumer will also be a .net framework program.
Otherwise its safe to use XML formatter and this way you can more easily read and write files. However be concern about the security threat as xml file is readable.

Here is a simple program.

Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml.Serialization;

namespace XMLSerializeDeserialize
{
    class Program
    {
        static void Main(string[] args)
        {
            /// Create XML Serialization
            FileStream fs = new FileStream("xml.XML", FileMode.Create);
            XmlSerializer xmls = new XmlSerializer(typeof(DateTime));
            xmls.Serialize(fs, DateTime.Now);
            fs.Close();
            /// End OF XML Creation
            /// start DESERIALIZATION

            FileStream fr = new FileStream("xml.XML",FileMode.Open);
            DateTime Pt = (DateTime) xmls.Deserialize(fr);
            fr.Close();
            Console.WriteLine(Pt.ToString());
            Console.Read();
        }
    }
}


The Generated XML:
Code:
<?xml version="1.0"?>
<dateTime>
   2009-05-18T21:51:42.46875+06:00
</dateTime>


Here i am serializing a date-time object. You can easily see that, the XML file is created under the current project directory [ specifically in the BIN folder under DEBUG].

Though you can also do some more complex serialization and Deserialization. You can also deserialize complex object property in runtime.

Hope we will talk about this in next post.

Happy CODING.
BE IN GRID.

Altaf Hussain
Software Engineer
LeadSoft Bangladesh Ltd.
Founder Member: CrissCrossComputers Ltd.
altaf_sust_82@yahoo.com


Offline
 Profile  
Top 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC + 6 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron