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 SlotModel Read(string filePath, string name, ClientSettings settings) { var slots = new List <SlotData>(); var serializer = new XmlFileSerializer <SlotSummary>(); try { slots = serializer.Deserialize(filePath).Slots; } catch (Exception ex) { _logger.ErrorFormat(ex, "{0}", ex.Message); } var slotData = slots.FirstOrDefault(x => x.GridData.Name == name); if (slotData != null) { var slot = AutoMapper.Mapper.Map <SlotData, SlotModel>(slotData); Protein protein = _proteinDictionary.GetProteinOrDownload(slotData.UnitInfo.ProjectID); // build unit info logic var unitInfoLogic = ServiceLocator.Resolve <UnitInfoLogic>(); unitInfoLogic.CurrentProtein = protein; unitInfoLogic.UnitInfoData = slotData.UnitInfo; slot.Prefs = _prefs; slot.Settings = settings; slot.UnitInfoLogic = unitInfoLogic; return(slot); } return(new SlotModel { Prefs = _prefs, Settings = settings }); }
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 SlotModel Read(string filePath, string name, ClientSettings settings) { var slots = new List<SlotData>(); var serializer = new XmlFileSerializer<SlotSummary>(); try { slots = serializer.Deserialize(filePath).Slots; } catch (Exception ex) { _logger.ErrorFormat(ex, "{0}", ex.Message); } var slotData = slots.FirstOrDefault(x => x.GridData.Name == name); if (slotData != null) { var slot = AutoMapper.Mapper.Map<SlotData, SlotModel>(slotData); Protein protein = _proteinDictionary.GetProteinOrDownload(slotData.UnitInfo.ProjectID); // build unit info logic var unitInfoLogic = ServiceLocator.Resolve<UnitInfoLogic>(); unitInfoLogic.CurrentProtein = protein; unitInfoLogic.UnitInfoData = slotData.UnitInfo; slot.Prefs = _prefs; slot.Settings = settings; slot.UnitInfoLogic = unitInfoLogic; return slot; } return new SlotModel { Prefs = _prefs, Settings = settings }; }
public void Deserialize_NonexistingPath_DefaultReturned() { // 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(); data.BoolDummyProperty = true; var xmlSerializer = new XmlFileSerializer(log); // Act var deserializedData = xmlSerializer.Deserialize <SerializableBoolDummy>(filePath); // Assert Assert.IsTrue(deserializedData == null); }
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)); }
// File IO public static AISDefnCollection LoadFromFile(string fileName) { if (fileName == "") { return(LoadInternal()); } AISDefnCollection aisDefnCol = XmlFileSerializer.Deserialize(fileName); if (aisDefnCol != null) { aisDefnCol.FileName = fileName; aisDefnCol.FileType = FileTypeEnum.NativeXMLFile; return(aisDefnCol); } return(LoadInternal()); }
// File IO public static N0183DefnCollection LoadFromFile(string fileName) { fileName = CommonRoutines.ResolveFileNameIfEmpty(fileName, DefDefnFile); if (fileName == "") { return(LoadInternal()); } N0183DefnCollection n0183DefnCol = XmlFileSerializer.Deserialize(fileName); if (n0183DefnCol != null) { n0183DefnCol.FileName = fileName; n0183DefnCol.FileType = FileTypeEnum.NativeXMLFile; return(n0183DefnCol); } return(LoadInternal()); }
// File IO public static PGNDefnCollection LoadFromFile(string fileName) { fileName = CommonRoutines.ResolveFileNameIfEmpty(fileName, DefDefnFile); if (fileName == "") { return(LoadInternal()); } PGNDefnCollection pgnDefnCol = XmlFileSerializer.Deserialize(fileName); if (pgnDefnCol != null) { pgnDefnCol.FileName = fileName; pgnDefnCol.FileDateTime = File.GetLastWriteTime(fileName); pgnDefnCol.FileType = FileTypeEnum.NativeXMLFile; return(pgnDefnCol); } return(LoadInternal()); }
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); }
// File IO public static ManufacturerCollection LoadFromFile() { string fileName = DefDefnFile; if (fileName == "") { return(LoadInternal()); } ManufacturerCollection manufCol = XmlFileSerializer.Deserialize(fileName); if (manufCol != null) { manufCol.FileName = fileName; manufCol.FileType = FileTypeEnum.NativeXMLFile; return(manufCol); } return(LoadInternal()); }
public static DisplaysCollection LoadFromFile(string fileName) { DisplaysCollection displays = XmlFileSerializer.Deserialize(fileName); if (displays != null) { displays.FileName = fileName; displays.FileDateTime = File.GetLastWriteTime(fileName); return(displays); } return(null); }
public Task <IEnumerable <RecordListInfo> > GetRecords() { return(Task <IEnumerable <RecordListInfo> > .Factory.StartNew(() => { if (!Directory.Exists(Paths.RecordsPath)) { return null; } var dirs = Directory.GetDirectories(Paths.RecordsPath); var records = new List <RecordListInfo>(); var availableTemplatesCount = _templatesManager.GetTemplates().Count(); foreach (var s in dirs) { var recordXmlPath = Path.Combine(s, "record.xml"); var recordInfo = new RecordListInfo(); var record = XmlFileSerializer.Deserialize <Record>(recordXmlPath); foreach (var recordEntry in record.Entries) { UpdateRecordFile(record.RecordDate, recordEntry.FilePath, Path.Combine(s, recordEntry.FilePath)); } record = XmlFileSerializer.Deserialize <Record>(recordXmlPath); var recordItems = record.Entries.Select(recordEntry => new RecordListItemInfo { DisplayName = recordEntry.DisplayName, FilePath = recordEntry.FilePath, TemplateId = recordEntry.TemplateId, IsFilledIn = recordEntry.IsFilledIn, RecordDate = record.RecordDate }) .ToList(); recordInfo.RecordDate = record.RecordDate; recordInfo.RecordsInfo = recordItems; recordInfo.CanAddEntries = recordItems.Count < availableTemplatesCount; recordInfo.RecordId = record.RecordId; records.Add(recordInfo); } return records; })); }
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 IEnumerable <TemplateInfo> GetTemplates(bool includeDisabled = false) { if (!Directory.Exists(Paths.TemplatesPath)) { return(null); } var files = Directory.GetFiles(Paths.TemplatesPath, "*.xml"); var templates = new List <TemplateInfo>(); foreach (var file in files) { var template = XmlFileSerializer.Deserialize <Template>(file); if (!includeDisabled && !template.IsEnabled) { continue; } templates.Add(TemplateMapper.ToTemplateInfo(template)); } return(templates); }
public IEnumerable <SlotData> Read(string filePath) { var serializer = new XmlFileSerializer <SlotSummary>(); return(serializer.Deserialize(filePath).Slots); }
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; } }
public IEnumerable<SlotData> Read(string filePath) { var serializer = new XmlFileSerializer<SlotSummary>(); return serializer.Deserialize(filePath).Slots; }