Esempio n. 1
0
        /// <summary>
        /// Gets the persistable object from XML persistence.
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static ProfilePersistable GetObjectFromPersistence(string fileName)
        {
            ProfilePersistable profileDataModel = null;

            try
            {
                if (System.IO.File.Exists(fileName))
                {
                    FileStream readFileStream = null;
                    try
                    {
                        // Create a new file stream for reading the XML file
                        readFileStream = new System.IO.FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);

                        // Create a new XmlSerializer instance with the type of the test class
                        XmlSerializer serializerObj = new XmlSerializer(typeof(ProfilePersistable));

                        // Load the object saved above by using the Deserialize function
                        profileDataModel = (ProfilePersistable)serializerObj.Deserialize(readFileStream);
                    }
                    catch (Exception e)
                    {
                        logger.Error(e);
                    }
                    finally
                    {
                        // Cleanup
                        if (readFileStream != null)
                        {
                            readFileStream.Close();
                        }
                    }
                }

                return(profileDataModel);
            }
            catch (Exception e)
            {
                logger.Error(e);
            }

            return(null);
        }
Esempio n. 2
0
        /// <summary>
        /// Saves the persistable object to XML persistence.
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="persistable"></param>
        /// <returns></returns>
        public static bool SaveObjectToPersistence(string fileName,
                                                   ProfilePersistable persistable)
        {
            XmlWriterSettings xws = new XmlWriterSettings();

            xws.NewLineOnAttributes = true;
            xws.Indent      = true;
            xws.IndentChars = "  ";
            xws.Encoding    = System.Text.Encoding.UTF8;

            // Create a new file stream to write the serialized object to a file
            XmlWriter xw = null;

            try
            {
                xw = XmlWriter.Create(fileName, xws);

                // Create a new XmlSerializer instance with the type of the test class
                XmlSerializer serializerObj = new XmlSerializer(persistable.GetType());
                serializerObj.Serialize(xw, persistable);

                return(true);
            }
            catch (Exception e)
            {
                logger.Error(e);
            }
            finally
            {
                if (xw != null)
                {
                    xw.Close(); // Cleanup
                }
            }

            return(false);
        }