I, Me, Myself

My photo
Hyderabad, Hindhu/AndhraPradesh, India
CoOl,jOvial,strAight fOrward

Search

Tuesday, June 7, 2011

Serilization

 public class SerializationHelper
    { 
        ///
        /// Method to Serialize Object
        ///

        ///
Object Entity Type
        /// seralization object
        /// Inner XML Data
        public static string Serialize(Type entityType, object seralizationObject)
        {
            string resultXML = string.Empty;           
            System.IO.MemoryStream stream = new System.IO.MemoryStream();
            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();

            try
            {
                XmlSerializer xmlSerializer = new XmlSerializer(entityType);
                xmlSerializer.Serialize(stream, seralizationObject);
                if (stream != null)
                {
                    stream.Position = 0;
                    doc.Load(stream);
                    resultXML = doc.InnerXml;
                }
            }
            catch
            {
                return string.Empty;
            }

            return resultXML;
        }

        ///
        /// Method to DeSerialize XMl to Object
        ///

        /// Object Entity Type
        /// Inner XML Data
        /// DeSerialized Object
        public static object DeSerialize(Type entityType, string xmlData)
        {
            object result = null;
            try
            {
                XmlSerializer xmlSerializer = new XmlSerializer(entityType);
                using (TextReader tr = new StringReader(xmlData))
                {
                    result = xmlSerializer.Deserialize(tr);
                }               
            }
            catch
            {
                return null;
            }

            return result;
        }


        public static void SerializeBOData(Type type, string xmlFile, object obj)
        {
            TextWriter textWriter = null;

            try
            {
                XmlSerializer xmlSerializer = new XmlSerializer(type);

                // Writing the file requires a StreamWriter.
                textWriter = new StreamWriter(xmlFile);

                // Serialize the BOData to xml.
                if (textWriter != null)
                {
                    xmlSerializer.Serialize(textWriter, obj);
                }
            }
            catch (Exception ex)
            {
                // Log the exception.
              
            }
            finally
            {
                if (textWriter != null)
                {
                    textWriter.Close();
                }
            }
        }

        public static object DeserializeXml(Type boDataType, string xmlFile)
        {
           
            TextReader textReader = null;

            try
            {
                if (!System.IO.File.Exists(xmlFile))
                {
                    return null;
                }

                XmlSerializer xmlSer = new XmlSerializer(boDataType);

                // Read the XML.
                textReader = new StreamReader(xmlFile);

                // Cast the deserialized xml to the type of BOData.
                return xmlSer.Deserialize(textReader);
            }
            catch (Exception ex)
            {
                // Propogate the exception.
              
            }
            finally
            {
                if (textReader != null)
                {
                    textReader.Close();
                    textReader = null;
                }
            }

            // Return the data transfer object.
            return null;
        }
    }

No comments:

Followers