Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Medicines.Data;
- using System;
- using System.Globalization;
- using System.IO;
- using System.Linq;
- using System.Xml.Linq;
- public class Deserializer
- {
- private readonly MedicinesContext _context;
- public Deserializer(MedicinesContext context)
- {
- _context = context;
- }
- public void ImportPharmaciesFromXml(string xmlFilePath)
- {
- try
- {
- XElement root = XElement.Load(xmlFilePath);
- var pharmacies = root.Elements("Pharmacy")
- .Select(pharmacyElement => new Pharmacy
- {
- Id = Convert.ToInt32(pharmacyElement.Element("Id").Value),
- Name = pharmacyElement.Element("Name").Value,
- PhoneNumber = pharmacyElement.Element("PhoneNumber").Value,
- IsNonStop = Convert.ToBoolean(pharmacyElement.Element("IsNonStop").Value),
- Medicines = pharmacyElement.Elements("Medicine")
- .Select(medicineElement => new Medicine
- {
- })
- .ToList()
- })
- .ToList();
- foreach (var pharmacy in pharmacies)
- {
- if (IsValidPharmacy(pharmacy))
- {
- _context.Pharmacies.Add(pharmacy);
- foreach (var medicine in pharmacy.Medicines)
- {
- if (IsValidMedicine(medicine, pharmacy.Id))
- {
- _context.Medicines.Add(medicine);
- }
- else
- {
- PrintErrorMessage($"Medicine {medicine.Name} with producer {medicine.Producer} has validation errors and was not imported.");
- }
- }
- }
- else
- {
- PrintErrorMessage($"Pharmacy {pharmacy.Name} has validation errors and was not imported.");
- }
- }
- _context.SaveChanges();
- }
- catch (Exception ex)
- {
- Console.WriteLine($"An error occurred while importing data from XML: {ex.Message}");
- }
- }
- private bool IsValidPharmacy(Pharmacy pharmacy)
- {
- }
- private bool IsValidMedicine(Medicine medicine, int currentPharmacyId)
- {
- }
- private void PrintErrorMessage(string message)
- {
- Console.WriteLine($"Invalid Data! {message}");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement