Serialization Utility : Serialization « File Stream « C# / C Sharp






Serialization Utility

        
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;

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

namespace Kenly.ChineseSegment.Core
{
    internal class SerializationUtility
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="target"></param>
        /// <returns></returns>
        public static bool SerializeToXml(string filePath, object target)
        {
            if (!IsValidSerializeArgs(filePath, target))
            {
                return false;
            }

            try
            {
                XmlSerializerFactory xmlSerializerFactory = new XmlSerializerFactory();
                XmlSerializer xmlSerializer = xmlSerializerFactory.CreateSerializer(target.GetType(), target.GetType().Name);
                SerializeToXml(filePath, target, xmlSerializer);
                return true;
            }
            catch(Exception ex)
            {
                throw new ApplicationException("Serialize xml file failed.", ex);
            }
        }

        private static void SerializeToXml(string filePath, object target, XmlSerializer xmlSerializer)
        {
            Stream stream = new FileStream(filePath, FileMode.OpenOrCreate);
            xmlSerializer.Serialize(stream, target);
            stream.Close();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="targetType"></param>
        /// <returns></returns>
        public static object DeserializeFromXml(string filePath, Type targetType)
        {
            if (!IsValidDeserializeArgs(filePath, targetType))
            {
                return null;
            }

            try
            {
                XmlSerializerFactory xmlSerializerFactory = new XmlSerializerFactory();
                XmlSerializer xmlSerializer = xmlSerializerFactory.CreateSerializer(targetType, targetType.Name);
                object obj = DeserializeFromXml(filePath, xmlSerializer);
                return obj;
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Deserialize xml file failed.", ex);
            }

        }

        private static object DeserializeFromXml(string filePath, XmlSerializer xmlSerializer)
        {
            Stream stream = new FileStream(filePath, FileMode.Open);
            object obj = xmlSerializer.Deserialize(stream);
            stream.Close();
            return obj;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="target"></param>
        /// <returns></returns>
        public static bool SerializeToBin(string filePath, object target)
        {
            if (!IsValidSerializeArgs(filePath, target))
            {
                return false;
            }

            try
            {
                BinaryFormatter formatter=new BinaryFormatter();

                SerializeObject(filePath, target, formatter);
                return true;
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Serialize bin file failed.", ex);
            }
        }

       
        /// <summary>
        /// 
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="targetType"></param>
        /// <returns></returns>
        public static object DeserializeFromBin(string filePath, Type targetType)
        {
            if (!IsValidDeserializeArgs(filePath, targetType))
            {
                return null;
            }

            try
            {
                BinaryFormatter formatter = new BinaryFormatter();

                object obj = DeserializeObject(filePath, formatter);
                return obj;
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Deserialize bin file failed.", ex);
            }

        }

        private static void SerializeObject(string filePath, object target, IFormatter formatter)
        {
            Stream stream = new FileStream(filePath, FileMode.OpenOrCreate);
            formatter.Serialize(stream, target);
            stream.Close();
        }

        private static object DeserializeObject(string filePath,IFormatter formatter)
        {
            Stream stream = new FileStream(filePath, FileMode.Open);
            object obj = formatter.Deserialize(stream);
            stream.Close();
            return obj;
        }

        private static bool IsValidSerializeArgs(string filePath, object target)
        {
            if (target == null)
            {
                return false;
            }
            if (string.IsNullOrEmpty(filePath))
            {
                return false;
            }

            return true;
        }

        private static bool IsValidDeserializeArgs(string filePath, Type targetType)
        {
            if (targetType == null)
            {
                return false;
            }

            if (!Utility.IsValidFile(filePath))
            {
                return false;
            }

            return true;
        }


    }
}

   
    
    
    
    
    
    
    
  








Related examples in the same category

1.Use Serializable attribute to mark a generic class
2.Use Serializable attribute to mark a class
3.Deserialize Object
4.Serialize and DeSerialize
5.Three types of Serialization: Binary, Soap, XML
6.Use XML Serialization with Custom ObjectsUse XML Serialization with Custom Objects
7.Working with the Serializable Attribute
8.NonSerialized attributesNonSerialized attributes
9.Serialize hiearchy classesSerialize hiearchy classes
10.C# Serialization C# Serialization
11.Serial Employee class
12.Set XML tag name for Serialization
13.illustrates binary serializationillustrates binary serialization
14.Deserialize
15.Collection Serialization
16.Serializes an object to binary
17.Deserializes an object from binary
18.Object to string serialization/Deserialization
19.Object to byte array serialization/Deserialization
20.Javascript Serializer
21.Serialize and Deserialize (2)
22.Binary Serializer
23.Deserialize the incomming data to the T datatype, this method used to deserialize the test cases data to the required entity
24.Serialization Utilities
25.Clone With Serialization