Tuesday, May 29, 2012

XML Serialization


         
Here is the sample code for XML serialization.


            XmlSerializer xmlSerializer = new XmlSerializer(typeof(Database));  //Database is a class file
            File.WriteAllText(filePath, string.Empty);
            stream = File.Open(filePath, FileMode.Open);
            xmlSerializer.Serialize(stream, dataBase);
            stream.Close();


Serialization means you are converting the object into a binary/ xml format and you can keep it in your locat disk, and whenever it is needed you can convert it into the object format.

Deserialization codes are as follows.


            Database dataBase = new Database();
           XmlSerializer xmlSerializer = new XmlSerializer(typeof(Database));
            if (File.Exists(filePath))
            {
                stream = File.Open(filePath, FileMode.Open);


                if (stream.Length > 0)
                {
                    dataBase = (Database)xmlSerializer.Deserialize(stream);
                }
               stream.Close();
               return dataBase;
             }


learn more about serialization

No comments:

Post a Comment