public void ReadTest() { // create sample data List <TestObject> data = new List <TestObject>(); data.Add(new TestObject() { Label = "FIRST", Date = DateTime.UtcNow.Date }); data.Add(new TestObject() { Label = "SECOND", Date = DateTime.UtcNow.AddDays(1).Date }); data.Add(new TestObject() { Label = "THIRD", Date = DateTime.UtcNow.AddDays(2).Date }); // initilaize reader XMLReaderWriter <List <TestObject> > xmlReader = new XMLReaderWriter <List <TestObject> >("XMLReaderTestFileRead.xml", _folderName); // save xmlReader.Save(data); // now load the data List <TestObject> readData = xmlReader.Read(); // check for data inside Assert.AreEqual(3, readData.Count()); for (int index = 0; index < 3; index++) { Assert.AreEqual(data[index].Date.Date, readData[index].Date.Date); Assert.AreEqual(data[index].Label, readData[index].Label); } }
/// <summary> /// Saves the days /// </summary> public void Save() { // get day data List <DayData> dayData = _dayDatas.Values.ToList(); // save XMLReaderWriter <List <DayData> > writer = new XMLReaderWriter <List <DayData> >("DayData.xml", "AppData"); writer.Save(dayData); }
/// <summary> /// Loads meta-data for all days.. /// </summary> private void Initialize() { XMLReaderWriter <List <DayMetadatum> > reader = new XMLReaderWriter <List <DayMetadatum> >("DayMetadata.xml", "AppData"); List <DayMetadatum> metadata = reader.Read(); if (metadata != null) { foreach (DayMetadatum metadatum in metadata) { _holidays.Add(metadatum.Date.Date, metadatum); } } }
/// <summary> /// Loads data for all days.. /// </summary> private void Initialize() { XMLReaderWriter <List <DayData> > reader = new XMLReaderWriter <List <DayData> >("DayData.xml", "AppData"); List <DayData> dayDatas = reader.Read(); if (dayDatas != null) { foreach (DayData dayData in dayDatas) { _dayDatas.Add(dayData.Date.Date, dayData); } } }
public void SaveTest() { // create sample data List <TestObject> data = new List <TestObject>(); data.Add(new TestObject() { Label = "FIRST", Date = DateTime.UtcNow.Date }); data.Add(new TestObject() { Label = "SECOND", Date = DateTime.UtcNow.AddDays(1).Date }); data.Add(new TestObject() { Label = "THIRD", Date = DateTime.UtcNow.AddDays(2).Date }); // initilaize reader string folderPath = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); folderPath = Path.Combine(folderPath, _folderName); XMLReaderWriter <List <TestObject> > xmlReader = new XMLReaderWriter <List <TestObject> >("XMLReaderTestFileSave.xml", _folderName); // save xmlReader.Save(data); // file path + name string filePath = Path.Combine(folderPath, "XMLReaderTestFileSave.xml"); // check if file exists Assert.IsTrue(File.Exists(filePath)); // load XDocument document = XDocument.Load(filePath); XElement xElement = document.Elements().First(); // check for data inside Assert.AreEqual(3, xElement.Elements().Count()); for (int index = 0; index < 3; index++) { Assert.AreEqual(data[index].Date.Date, Convert.ToDateTime(xElement.Elements().ElementAt(index).Element("Date").Value).Date); Assert.AreEqual(data[index].Label, xElement.Elements().ElementAt(index).Element("Label").Value.ToString()); } }