Ejemplo n.º 1
0
        /// <summary>
        /// Deserializes a datafile
        /// </summary>
        /// <typeparam name="T">The type to deserialize from the datafile</typeparam>
        /// <param name="filename">The datafile name</param>
        /// <returns></returns>
        internal static T DeserializeDatafile <T>(string filename)
        {
            // Gets the full path
            string path = Datafile.GetFullPath(filename);

            try
            {
                // Deserializes
                using (var s = FileHelper.OpenRead(path, false))
                {
                    using (GZipStream zs = new GZipStream(s, CompressionMode.Decompress))
                    {
                        XmlSerializer xs = new XmlSerializer(typeof(T));
                        return((T)xs.Deserialize(zs));
                    }
                }
            }
            catch (InvalidOperationException ex)
            {
                String message = String.Format("An error occured decompressing {0}, the error message was '{1}' from '{2}'. Try deleting all of the xml.gz files in %APPDATA%\\EVEMon.", filename, ex.Message, ex.Source);
                throw new ApplicationException(message, ex);
            }
            catch (XmlException ex)
            {
                String message = String.Format("An error occured reading the XML from {0}, the error message was '{1}' from '{2}'. Try deleting all of the xml.gz files in %APPDATA%\\EVEMon.", filename, ex.Message, ex.Source);
                throw new ApplicationException(message, ex);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Deserializes a datafile.
        /// </summary>
        /// <typeparam name="T">The type to deserialize from the datafile</typeparam>
        /// <param name="filename">The datafile name</param>
        /// <param name="transform"></param>
        /// <returns></returns>
        internal static T DeserializeDatafile <T>(string filename, XslCompiledTransform transform = null)
        {
            // Gets the full path
            string path = Datafile.GetFullPath(filename);

            try
            {
                using (Stream stream = FileHelper.OpenRead(path, false))
                {
                    GZipStream    gZipStream = new GZipStream(stream, CompressionMode.Decompress);
                    XmlSerializer xs         = new XmlSerializer(typeof(T));

                    // Deserialization without transform
                    if (transform == null)
                    {
                        return((T)xs.Deserialize(gZipStream));
                    }

                    // Deserialization with transform
                    MemoryStream  memoryStream = GetMemoryStream();
                    XmlTextWriter writer       = new XmlTextWriter(memoryStream, Encoding.UTF8);
                    XmlTextReader reader       = new XmlTextReader(gZipStream);

                    // Apply the XSL transform
                    writer.Formatting = Formatting.Indented;
                    transform.Transform(reader, writer);
                    writer.Flush();

                    // Deserialize from the given stream
                    memoryStream.Seek(0, SeekOrigin.Begin);
                    return((T)xs.Deserialize(memoryStream));
                }
            }
            catch (InvalidOperationException ex)
            {
                String message =
                    $"An error occurred decompressing {filename}, the error message was '{ex.Message}' from '{ex.Source}'. " +
                    $"Try deleting all of the {Datafile.DatafilesExtension} files in %APPDATA%\\EVEMon.";
                throw new InvalidOperationException(message, ex);
            }
            catch (XmlException ex)
            {
                String message =
                    $"An error occurred reading the XML from {filename}, the error message was '{ex.Message}' from '{ex.Source}'. " +
                    $"Try deleting all of the {Datafile.DatafilesExtension} files in %APPDATA%\\EVEMon.";
                throw new XmlException(message, ex);
            }
        }