Beispiel #1
0
        public void TestStoreRestore()
        {
            using (var stream = new MemoryStream())
            {
                using (var writer = XmlWriter.Create(stream))
                {
                    writer.WriteStartElement("Test");
                    var settings = new QuickFiltersSettings
                    {
                        TimeFilter = { SpecialInterval = SpecialDateTimeInterval.ThisWeek }
                    };
                    settings.Add(new QuickFilterSettings {
                        Value = "42"
                    });
                    settings.Save(writer);
                    writer.WriteEndElement();
                }

                stream.Position = 0;
                //Console.WriteLine(Encoding.UTF8.GetString(stream.ToArray()));

                using (var reader = XmlReader.Create(stream))
                {
                    reader.MoveToContent();

                    var settings = new QuickFiltersSettings();
                    settings.Restore(reader);
                    settings.TimeFilter.SpecialInterval.Should().Be(SpecialDateTimeInterval.ThisWeek);
                    settings.Should().HaveCount(1);
                    settings[0].Value.Should().Be("42");
                }
            }
        }
        public bool Save()
        {
            try
            {
                using (var stream = new MemoryStream())
                {
                    var settings = new XmlWriterSettings
                    {
                        Indent          = true,
                        IndentChars     = "  ",
                        NewLineChars    = "\r\n",
                        NewLineHandling = NewLineHandling.Replace
                    };
                    using (XmlWriter writer = XmlWriter.Create(stream, settings))
                    {
                        writer.WriteStartElement("xml");

                        writer.WriteStartElement("mainwindow");
                        _mainWindow.Save(writer);
                        writer.WriteEndElement();

                        writer.WriteStartElement("datasources");
                        _dataSources.Save(writer);
                        writer.WriteEndElement();

                        writer.WriteStartElement("quickfilters");
                        _quickFilters.Save(writer);
                        writer.WriteEndElement();

                        writer.WriteStartElement("autoupdate");
                        _autoUpdate.Save(writer);
                        writer.WriteEndElement();

                        writer.WriteStartElement("export");
                        _export.Save(writer);
                        writer.WriteEndElement();

                        writer.WriteStartElement("logviewer");
                        _logViewer.Save(writer);
                        writer.WriteEndElement();

                        writer.WriteStartElement("logfile");
                        _logFile.Save(writer);
                        writer.WriteEndElement();

                        writer.WriteStartElement("customformats");
                        _customFormats.Save(writer);
                        writer.WriteEndElement();

                        writer.WriteEndElement();
                    }

                    if (!Directory.Exists(_fileFolder))
                    {
                        Directory.CreateDirectory(_fileFolder);
                    }

                    using (var file = new FileStream(_fileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))
                    {
                        var length = (int)stream.Position;
                        file.Write(stream.GetBuffer(), 0, length);
                        file.SetLength(length);
                    }

                    return(true);
                }
            }
            catch (Exception e)
            {
                Log.ErrorFormat("Caught unexpected exception: {0}", e);
                return(false);
            }
        }