Ejemplo n.º 1
0
        /// <summary>
        /// Serializes the experiment to XML file.
        /// </summary>
        /// <param name="experiment">The experiment to be saved.</param>
        /// <param name="filename">The filename.</param>
        /// <returns>true if saving was successful, otherwise false</returns>
        private static bool SerializeExperimentToXml(IExperiment experiment, string filename)
        {
            bool success = false;

            try
            {
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent             = true;
                settings.NamespaceHandling  = NamespaceHandling.OmitDuplicates;
                settings.OmitXmlDeclaration = true;

                //create the xml writer
                using (XmlWriter writer = XmlWriter.Create(filename, settings))
                {
                    ExperimentSerializer.SerializeExperiment(writer, experiment);
                }

                success = true;
            }
            catch (System.Security.SecurityException ex)
            {
                System.Diagnostics.Debug.Write("Security exception while serializing experiment to Xml " + ex);
            }

            return(success);
        }
Ejemplo n.º 2
0
        private static bool SerializeExperimentToStream(IExperiment experiment, string filename)
        {
            bool success = false;

            try
            {
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent             = true;
                settings.NamespaceHandling  = NamespaceHandling.OmitDuplicates;
                settings.OmitXmlDeclaration = true;
                settings.ConformanceLevel   = ConformanceLevel.Fragment;
                settings.CloseOutput        = false;

                //create the xml writer
                MemoryStream fileStream = new MemoryStream();
                using (XmlWriter writer = XmlWriter.Create(fileStream, settings))
                {
                    ExperimentSerializer.SerializeExperiment(writer, experiment);
                    writer.Flush();
                    writer.Close();

                    //we enrcypt the file before save it
                    Crypto.EncryptFile(fileStream, filename);
                }

                success = true;
            }
            catch (System.Security.SecurityException ex)
            {
                System.Diagnostics.Debug.Write("Security exception while serializing experiment to Xml " + ex);
            }

            return(success);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Loads a experiment from the specified file.
        /// </summary>
        /// <param name="filename">The filename.</param>
        /// <exception cref="TraceLab.Core.Exceptions.ExperimentLoadException">throws if experiment load fails</exception>
        /// <returns>
        /// Returns loaded m_experiment. If loading failed it returns null.
        /// </returns>
        public static Experiment Load(string fileName, TraceLab.Core.Components.ComponentsLibrary library)
        {
            Experiment experiment = null;

            try
            {
                using (System.Xml.XmlReader reader = System.Xml.XmlReader.Create(fileName))
                {
                    experiment = ExperimentSerializer.DeserializeExperiment(reader, library, fileName);
                }

                if (experiment != null)
                {
                    experiment.ResetModifiedFlag();
                }
            }
            catch (ArgumentException e)
            {
                throw new ExperimentLoadException("The experiment file could not be loaded. Filename cannot be empty. ", e);
            }
            catch (System.Security.SecurityException e)
            {
                throw new ExperimentLoadException("The experiment file could not be loaded.", e);
            }
            catch (System.IO.FileNotFoundException e)
            {
                throw new ExperimentLoadException("The experiment file has not been found.", e);
            }
            catch (System.IO.DirectoryNotFoundException e)
            {
                throw new ExperimentLoadException("The directory has not been found.", e);
            }
            catch (UriFormatException e)
            {
                throw new ExperimentLoadException("The experiment is corrupted and could not be loaded.", e);
            }
            catch (System.Xml.XmlException e)
            {
                throw new ExperimentLoadException("The experiment is corrupted and could not be loaded.", e);
            }

            return(experiment);
        }