コード例 #1
0
        /// <summary>
        ///
        ///</summary>
        ///<param name="serializationType"></param>
        ///<returns></returns>
        public byte[] Save(SerializationTypeEnum serializationType)
        {
            byte[] ret = null;
            try
            {
                XmlDocument xmlDocument = new XmlDocument();

                XmlElement xRoot = xmlDocument.CreateElement("StockChartX");
                xmlDocument.AppendChild(xRoot);

                ConvertToXmlEx("Version", xRoot, xmlDocument, Version);
                XmlAttribute xmlAttribute = xmlDocument.CreateAttribute("SaveType");
                xmlAttribute.Value = serializationType.ToString();
                xRoot.Attributes.Append(xmlAttribute);

                switch (serializationType)
                {
                case SerializationTypeEnum.All:
                    SaveWorkSpace(xRoot, xmlDocument);
                    SaveChartPanels(xRoot, xmlDocument);
                    SaveSeries(xRoot, xmlDocument);
                    SaveSeriesData(xRoot, xmlDocument);
                    SaveIndicators(xRoot, xmlDocument);
                    SaveLineStudies(xRoot, xmlDocument);
                    break;

                case SerializationTypeEnum.General:
                    SaveWorkSpace(xRoot, xmlDocument);
                    SaveIndicators(xRoot, xmlDocument);
                    SaveLineStudies(xRoot, xmlDocument);
                    break;

                case SerializationTypeEnum.Objects:
                    SaveLineStudies(xRoot, xmlDocument);
                    SaveIndicators(xRoot, xmlDocument);
                    break;

                case SerializationTypeEnum.Indicators:
                    SaveIndicators(xRoot, xmlDocument);
                    break;
                }

                using (MemoryStream stream = new MemoryStream())
                {
                    using (XmlTextWriter xmlWriter = new XmlTextWriter(stream, Encoding.UTF8)
                    {
                        Formatting = Formatting.Indented
                    })
                    {
                        xmlDocument.Save(xmlWriter);
                        ret = stream.ToArray();
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
            return(ret);
        }
コード例 #2
0
        ///<summary>
        /// Save's the chart into a given file name.
        ///</summary>
        ///<param name="filename">File name where to save</param>
        ///<param name="serializationType">Type of serialization to use.</param>
        /// <returns>true - if file was succesfully saved. false  - otherwise</returns>
        public bool SaveFile(string filename, SerializationTypeEnum serializationType)
        {
            try
            {
                XmlDocument xmlDocument = new XmlDocument();

                XmlElement xRoot = xmlDocument.CreateElement("StockChartX");
                xmlDocument.AppendChild(xRoot);

                ConvertToXmlEx("Version", xRoot, xmlDocument, Version);
                XmlAttribute xmlAttribute = xmlDocument.CreateAttribute("SaveType");
                xmlAttribute.Value = serializationType.ToString();
                xRoot.Attributes.Append(xmlAttribute);

                switch (serializationType)
                {
                case SerializationTypeEnum.All:
                    SaveWorkSpace(xRoot, xmlDocument);
                    SaveChartPanels(xRoot, xmlDocument);
                    SaveSeries(xRoot, xmlDocument);
                    SaveSeriesData(xRoot, xmlDocument);
                    SaveIndicators(xRoot, xmlDocument);
                    SaveLineStudies(xRoot, xmlDocument);
                    break;

                case SerializationTypeEnum.General:
                    SaveWorkSpace(xRoot, xmlDocument);
                    SaveIndicators(xRoot, xmlDocument);
                    SaveLineStudies(xRoot, xmlDocument);
                    break;

                case SerializationTypeEnum.Objects:
                    SaveLineStudies(xRoot, xmlDocument);
                    SaveIndicators(xRoot, xmlDocument);
                    break;

                case SerializationTypeEnum.Indicators:
                    SaveIndicators(xRoot, xmlDocument);
                    break;
                }

                using (FileStream fileStream = File.Create(filename))
                {
                    using (GZipStream zipStream = new GZipStream(fileStream, CompressionMode.Compress))
                    {
                        using (XmlTextWriter xmlWriter = new XmlTextWriter(zipStream, Encoding.UTF8)
                        {
                            Formatting = Formatting.Indented
                        })
                        {
                            xmlDocument.Save(xmlWriter);
                        }
                    }
                }

                return(true);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                return(false);
            }
        }