Пример #1
0
        /// <summary>
        /// Input to this DeserializeJsonFromFile is SerializationEntity which consists of FilePath, FileExtention etc
        /// And returns the Type of the Custom Class (Deserialized using the FilePath and FileExtension Mapped in
        /// the SerializationEntity).
        /// </summary>
        /// <param name="pobjSerializationEntity"></param>
        /// <returns></returns>
        public static T DeserializeJsonFromFile(SerializationEntity pobjSerializationEntity)
        {
            T      t = default(T);
            string lstrActualFilePath = pobjSerializationEntity.FilePath.Trim() + "." + pobjSerializationEntity.FileExtension;

            if (File.Exists(lstrActualFilePath))
            {
                StreamReader ss  = new StreamReader(lstrActualFilePath);
                string       str = ss.ReadToEnd();
                MemoryStream ms  = new MemoryStream(Encoding.UTF8.GetBytes(str));
                DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
                t = (T)ser.ReadObject(ms);
                ss.Close();
                ms.Close();
            }
            else
            {
                if (string.IsNullOrEmpty(pobjSerializationEntity.FilePath))
                {
                    throw new ArgumentException("Source :DeserializeJsonFromFile,FilePath cannot be Empty ,Kindly Specify a Valid File Path.");
                }
                else
                {
                    throw new ArgumentException("Source :DeserializeJsonFromFile,File Doesnt Exist!!!");
                }
            }
            return(t);
        }
        /// <summary>
        /// Input to this Custom Class, is SerializationEntity which consists of FilePath, FileExtention etc ,
        /// And Returns the Type of the Custom Class (Deserialized from the FilePath and FileExtension Mapped in
        /// the SerializationEntity)
        /// </summary>
        /// <param name="pobjSerializationEntity"></param>
        /// <returns></returns>
        public static T DeserializeFromXmlFile(SerializationEntity pobjSerializationEntity)
        {
            //Console.WriteLine("Entered DeserializeFromXmlFile Method ");
            T t = default(T);

            try
            {
                string lstrActualFilePath = pobjSerializationEntity.FilePath.Trim() + "." + pobjSerializationEntity.FileExtension;
                //Console.WriteLine("Actual File Path -- " + lstrActualFilePath);
                if (File.Exists(lstrActualFilePath))
                {
                    XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
                    TextReader    textReader    = new StreamReader(lstrActualFilePath);
                    t = (T)xmlSerializer.Deserialize(textReader);
                    textReader.Close();
                    //Console.WriteLine("Finished DeserializeFromXmlFile Method ");
                }
                else
                {
                    if (string.IsNullOrEmpty(pobjSerializationEntity.FilePath))
                    {
                        throw new ArgumentException("Source :XMLSerializationHelper,FilePath cannot be Empty ,Kindly Specify a Valid File Path.");
                    }
                    else
                    {
                        throw new ArgumentException("Source :XMLSerializationHelper,File Doesnt Exist!!!");
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception from DeserializeFromXmlFile Method: " + ex.StackTrace);
            }
            //Console.ReadKey();

            return(t);
        }