public void TestStoreRestore() { using (var stream = new MemoryStream()) { using (var writer = XmlWriter.Create(stream)) { writer.WriteStartElement("Test"); var settings = new QuickFilters { TimeFilter = { SpecialInterval = SpecialDateTimeInterval.ThisWeek } }; settings.Add(new QuickFilter { 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 QuickFilters(); settings.Restore(reader); settings.TimeFilter.SpecialInterval.Should().Be(SpecialDateTimeInterval.ThisWeek); settings.Should().HaveCount(1); settings[0].Value.Should().Be("42"); } } }
/// <summary> /// </summary> /// <param name="neededPatching">Whether or not certain values need to be changed (for example due to upgrades to the format - it is advised that the current settings be saved again if this is set to true)</param> public void Restore(out bool neededPatching) { neededPatching = false; if (!File.Exists(_fileName)) { return; } try { using (FileStream stream = File.OpenRead(_fileName)) using (XmlReader reader = XmlReader.Create(stream)) { while (reader.Read()) { switch (reader.Name) { case "mainwindow": _mainWindow.Restore(reader); break; case "datasources": bool dataSourcesNeededPatching; _dataSources.Restore(reader, out dataSourcesNeededPatching); neededPatching |= dataSourcesNeededPatching; break; case "quickfilters": _quickFilters.Restore(reader); break; case "autoupdate": _autoUpdate.Restore(reader); break; case "export": _export.Restore(reader); break; case "logviewer": _logViewer.Restore(reader); break; case "logfile": _logFile.Restore(reader); break; } } } } catch (Exception e) { Log.ErrorFormat("Caught unexpected exception: {0}", e); } }