public void Serialize_NonexistingPath_ExceptionThrown() { // Arrange var fileName = $"{Path.GetRandomFileName()}.xml"; var path = Path.Combine(Path.GetTempPath(), Path.Combine(Path.GetRandomFileName(), Path.GetRandomFileName())); var filePath = Path.Combine(path, fileName); if (Directory.Exists(path)) { Directory.Delete(path, true); } if (File.Exists(filePath)) { File.Delete(filePath); } var data = new SerializableBoolDummy(); var xmlSerializer = new XmlFileSerializer(log); // Act var serializationResult = xmlSerializer.Serialize(filePath, data); // Assert phase is empty: expecting exception, nothing to assert }
public void SerializeDeserialize_ExistingPath_Deserialized() { // Arrange var fileName = $"{Path.GetRandomFileName()}.xml"; var path = Path.GetTempPath(); var filePath = Path.Combine(path, fileName); Directory.CreateDirectory(path); if (File.Exists(filePath)) { File.Delete(filePath); } var data = new SerializableBoolDummy(); data.BoolDummyProperty = true; var defaultData = default(SerializableBoolDummy); var xmlSerializer = new XmlFileSerializer(log); // Act var serializationResult = xmlSerializer.Serialize(filePath, data); var deserializedData = xmlSerializer.Deserialize <SerializableBoolDummy>(filePath); // Assert Assert.IsTrue(serializationResult); Assert.IsFalse(deserializedData.Equals(defaultData)); Assert.IsTrue(deserializedData.Equals(data)); }
public void Serialize_ExistingPath_FileCreated() { // Arrange var fileName = $"{Path.GetRandomFileName()}.xml"; var path = Path.GetTempPath(); var filePath = Path.Combine(path, fileName); Directory.CreateDirectory(path); if (File.Exists(filePath)) { File.Delete(filePath); } var data = new SerializableBoolDummy(); var xmlSerializer = new XmlFileSerializer(log); // Act var serializationResult = xmlSerializer.Serialize(filePath, data); // Assert Assert.IsTrue(File.Exists(filePath)); Assert.IsTrue(serializationResult); }
private ICollection <string> GenerateXmlInternal(string folderPath, ICollection <SlotModel> slots) { Debug.Assert(!String.IsNullOrEmpty(folderPath)); Debug.Assert(slots != null); var fileList = new List <string>(); DateTime updateDateTime = DateTime.Now; var slotSummary = CreateSlotSummary(slots, updateDateTime); var serializer = new XmlFileSerializer <SlotSummary>(); string filePath = Path.Combine(folderPath, "SlotSummary.xml"); fileList.Add(filePath); serializer.Serialize(filePath, slotSummary); var slotDetailSerializer = new XmlFileSerializer <SlotDetail>(); foreach (var slot in slots) { var slotDetail = CreateSlotDetail(slot, updateDateTime); filePath = Path.Combine(folderPath, String.Concat(slot.Name, ".xml")); fileList.Add(filePath); slotDetailSerializer.Serialize(filePath, slotDetail); } return(fileList.AsReadOnly()); }
public void Deserialize_IncorrectDeserializationType_DefaultReturned() { // Arrange var fileName = $"{Path.GetRandomFileName()}.xml"; var path = Path.GetTempPath(); var filePath = Path.Combine(path, fileName); Directory.CreateDirectory(path); if (File.Exists(filePath)) { File.Delete(filePath); } var data = new SerializableBoolDummy(); data.BoolDummyProperty = true; var xmlSerializer = new XmlFileSerializer(log); // Act var serializationResult = xmlSerializer.Serialize(filePath, data); var deserializedData = xmlSerializer.Deserialize <SerializableIntDummy>(filePath); // Assert Assert.IsTrue(deserializedData == null); }
public void CreateOrUpdateTemplate(TemplateInfo t) { var path = Path.Combine(Paths.TemplatesPath, Path.GetFileNameWithoutExtension(t.TemplateFilePath) + ".xml"); var templates = GetTemplates(true); if (string.IsNullOrEmpty(t.Id)) { if (File.Exists(path) || File.Exists(Path.Combine(Paths.TemplatesPath, Path.GetFileName(t.TemplateFilePath) ?? throw new InvalidOperationException()))) { throw new Exception("Szablon o takiej nazwie już istnieje"); } t.Id = Guid.NewGuid().ToString(); var fName = Path.GetFileName(t.TemplateFilePath); File.Copy(t.TemplateFilePath, Path.Combine(Paths.TemplatesPath, fName)); t.TemplateFilePath = fName; } else { var item = templates.SingleOrDefault(x => string.Equals(x.Id, t.Id)); if (item != null) { File.Delete(Path.Combine(Paths.TemplatesPath, item.Name + ".xml")); } } XmlFileSerializer.Serialize(TemplateMapper.ToTemplate(t), path); }
public void WriteAndReadXmlTest() { var data1 = CreateTestList(); var serializer = new XmlFileSerializer<List<ProteinBenchmark>>(); serializer.Serialize("TestProteinBenchmark.xml", data1); var data2 = serializer.Deserialize("TestProteinBenchmark.xml"); ValidateTestList(data2); }
public void WriteAndReadXmlTest() { var data1 = CreateTestList(); var serializer = new XmlFileSerializer<List<UnitInfo>>(); serializer.Serialize("TestUnitInfo.xml", data1); var data2 = serializer.Deserialize("TestUnitInfo.xml"); Assert.IsTrue(data1.SequenceEqual(data2)); }
public void WriteAndReadXmlTest() { var data1 = CreateTestList(); var serializer = new XmlFileSerializer <List <ProteinBenchmark> >(); serializer.Serialize("TestProteinBenchmark.xml", data1); var data2 = serializer.Deserialize("TestProteinBenchmark.xml"); ValidateTestList(data2); }
public bool SaveToFile(string fileName) { // using (new WaitCursor()) { if (XmlFileSerializer.Serialize(fileName, this)) { FileName = fileName; return(true); } else { return(false); } } }
public bool SaveToFile(string fileName) { using (new WaitCursor()) { if (XmlFileSerializer.Serialize(fileName, this)) { FileName = fileName; FileType = FileTypeEnum.NativeXMLFile; return(true); } else { return(false); } } }
private static void UpdateRecordFile(DateTime recordDate, string fileName, string fullPath) { var hasBeenUpdated = false; var fInfo = new FileInfo(fullPath); using (var package = new ExcelPackage(fInfo)) { if (package.Workbook.Worksheets == null || package.Workbook.Worksheets.Count == 0) { throw new Exception("Plik '" + fileName + "' ma nieprawidłową strukturę. Nie odnaleziono żadnej zakładki"); } var sheet = package.Workbook.Worksheets[1]; if (sheet.Dimension.Rows > 1 && sheet.Cells[2, 1]?.Value != null) { hasBeenUpdated = true; } } var recordFile = Path.Combine(Paths.RecordsPath, recordDate.ToRecordDateString(), "record.xml"); var record = XmlFileSerializer.Deserialize <Record>(recordFile); if (record.Entries == null || record.Entries.Length <= 0) { return; } foreach (var t in record.Entries) { if (string.Equals(t.FilePath?.ToLower(), fileName.ToLower())) { t.IsFilledIn = hasBeenUpdated; } } XmlFileSerializer.Serialize(record, recordFile); }
public void CreateOrUpdateRecord(RecordInfo recordInfo) { if (recordInfo == null) { throw new Exception("Obiekt nowej inwentaryzacji nie został zdefiniowany"); } if (recordInfo.RecordsDate == null) { throw new Exception("Data inwentaryzacji nie może być pusta!"); } if (recordInfo.RecordTypes == null) { throw new Exception("Nie wskazano szablonów inwentaryzacji"); } var recordsPath = Path.Combine(Paths.RecordsPath, recordInfo.RecordsDate.ToRecordDateString()); var recordInfoFilePath = Path.Combine(recordsPath, "record.xml"); var templates = recordInfo.RecordTypes.Where(x => x.IsSelected).Select(x => x.TemplateInfo); try { if (string.IsNullOrEmpty(recordInfo.Id)) { if (Directory.Exists(recordsPath)) { throw new Exception("Inwentaryzacje na dzień " + recordInfo.RecordsDate.ToRecordDateString() + " już istnieją! Proszę wybrać inną datę lub zmodyfikować istniejące zestawienia"); } Directory.CreateDirectory(recordsPath); var record = new Record(); var entries = new List <RecordEntry>(); foreach (var templateInfo in templates) { var templateFullPath = Path.Combine(Paths.TemplatesPath, templateInfo.TemplateFilePath); File.Copy(templateFullPath, Path.Combine(recordsPath, templateInfo.TemplateFilePath)); entries.Add(new RecordEntry { DisplayName = templateInfo.Name, FilePath = templateInfo.TemplateFilePath, TemplateId = templateInfo.Id }); } record.Entries = entries.ToArray(); record.RecordId = Guid.NewGuid().ToString(); record.RecordDate = recordInfo.RecordsDate.Value; XmlFileSerializer.Serialize(record, recordInfoFilePath); } else { var record = XmlFileSerializer.Deserialize <Record>(recordInfoFilePath); var existingIds = record.Entries.Select(x => x.TemplateId).ToList(); var entriesToAdd = new List <RecordEntry>(); foreach (var templateInfo in templates) { if (!existingIds.Contains(templateInfo.Id)) { var templateFullPath = Path.Combine(Paths.TemplatesPath, templateInfo.TemplateFilePath); File.Copy(templateFullPath, Path.Combine(recordsPath, templateInfo.TemplateFilePath)); entriesToAdd.Add(new RecordEntry { DisplayName = templateInfo.Name, FilePath = templateInfo.TemplateFilePath, TemplateId = templateInfo.Id }); } } var entries = record.Entries.ToList(); entries.AddRange(entriesToAdd); record.Entries = entries.ToArray(); XmlFileSerializer.Serialize(record, recordInfoFilePath); } } catch { Directory.Delete(recordsPath, true); throw; } }
private IEnumerable<string> DoXmlGeneration(string folderPath, IEnumerable<SlotModel> slots) { Debug.Assert(!String.IsNullOrEmpty(folderPath)); Debug.Assert(slots != null); var fileList = new List<string>(); DateTime updateDateTime = DateTime.Now; var slotSummary = CreateSlotSummary(slots, updateDateTime); var serializer = new XmlFileSerializer<SlotSummary>(); string filePath = Path.Combine(Path.GetTempPath(), "SlotSummary.xml"); fileList.Add(filePath); serializer.Serialize(filePath, slotSummary); var slotDetailSerializer = new XmlFileSerializer<SlotDetail>(); foreach (var slot in slots) { var slotDetail = CreateSlotDetail(slot, updateDateTime); filePath = Path.Combine(Path.GetTempPath(), String.Concat(slot.Name, ".xml")); fileList.Add(filePath); slotDetailSerializer.Serialize(filePath, slotDetail); } return fileList.AsReadOnly(); }