public void ShouldParseGames() { // setup var env = new TestEnvironment(Logger); var menu = new PinballXMenu(); menu.Games.Add(new PinballXGame() { Filename = "Test_Game", Description = "Test Game (Test 2016)" }); env.Directory.Setup(d => d.Exists(TestEnvironment.VisualPinballDatabasePath)).Returns(true); env.Directory.Setup(d => d.GetFiles(TestEnvironment.VisualPinballDatabasePath)).Returns(new [] { Path.GetFileName(TestEnvironment.VisualPinballDatabaseXmlPath) }); env.MarshallManager.Setup(m => m.UnmarshallXml("Visual Pinball.xml")).Returns(menu); var menuManager = env.Locator.GetService <IMenuManager>(); // test menuManager.Initialize(); // assert menuManager.Systems.ToList().Should().NotBeEmpty().And.HaveCount(1); var system = menuManager.Systems[0]; system.Games.Should().NotBeEmpty().And.HaveCount(1); //system.Games[0].Filename.Should().Be("Test_Game"); //system.Games[0].Description.Should().Be("Test Game (Test 2016)"); //system.Games[0].DatabaseFile.Should().Be(Path.GetFileName(TestEnvironment.VisualPinballDatabaseXmlPath)); }
public void MarshallXml(PinballXMenu menu, string filepath) { try { var serializer = new XmlSerializer(typeof(PinballXMenu)); using (TextWriter writer = new StreamWriter(filepath)) { serializer.Serialize(writer, menu); _logger.Info("Saved {0}.", filepath); } } catch (Exception e) { _logger.Error(e, "Error writing XML to {0}: {1}", filepath, e.Message); _crashManager.Report(e, "xml"); } }
public PinballXMenu UnmarshallXml(string filepath) { var menu = new PinballXMenu(); if (!_file.Exists(filepath)) { return(menu); } Stream reader = null; try { var serializer = new XmlSerializer(typeof(PinballXMenu)); reader = new FileStream(filepath, FileMode.Open); menu = serializer.Deserialize(reader) as PinballXMenu; } catch (FileNotFoundException) { // ignore: http://stackoverflow.com/questions/1127431/xmlserializer-giving-filenotfoundexception-at-constructor } catch (Exception e) { _logger.Error(e, "Error parsing {0}: {1}", filepath, e.Message); _crashManager.Report(e, "xml"); } finally { reader?.Close(); } return(menu); }