public static void ReadUsersXml() { var xmlString = File.ReadAllText("../../../xml/users.xml"); var serializer = new XmlSerializer(typeof(UserDto[]), new XmlRootAttribute("users")); var deserializer = (UserDto[])serializer.Deserialize(new StringReader(xmlString)); var mapperConfig = new MapperConfiguration(cfg => { cfg.AddProfile <XmlProcessingExerciseProfile>(); }); var mapper = mapperConfig.CreateMapper(); //var users = new List<User>(); foreach (UserDto userDto in deserializer) { if (IsValid(userDto) == false) { Console.WriteLine("Invalid user data"); continue; } var user = mapper.Map <User>(userDto); //users.Add(user); var context = new XmlProcessingExerciseContext(); context.Add(user); context.SaveChanges(); } Console.WriteLine("Users added successfully!"); }
public static void ReadCategoriesXml() { var xmlString = File.ReadAllText("../../../xml/categories.xml"); var serializer = new XmlSerializer(typeof(CategoryDto[]), new XmlRootAttribute("categories")); var deserializer = (CategoryDto[])serializer.Deserialize(new StringReader(xmlString)); var mapperConfig = new MapperConfiguration(cfg => { cfg.AddProfile <XmlProcessingExerciseProfile>(); }); var mapper = mapperConfig.CreateMapper(); //var categories = new List<Category>(); foreach (CategoryDto categoryDto in deserializer) { if (IsValid(categoryDto) == false) { Console.WriteLine("Invalid category data"); continue; } var category = mapper.Map <Category>(categoryDto); var context = new XmlProcessingExerciseContext(); context.Add(category); context.SaveChanges(); } Console.WriteLine("Categories added successfully!"); }
public static void ReadProductsXml() { var xmlString = File.ReadAllText("../../../xml/products.xml"); var serializer = new XmlSerializer(typeof(ProductDto[]), new XmlRootAttribute("products")); var deserializer = (ProductDto[])serializer.Deserialize(new StringReader(xmlString)); var mapperConfig = new MapperConfiguration(cfg => { cfg.AddProfile <XmlProcessingExerciseProfile>(); }); var mapper = mapperConfig.CreateMapper(); //var products = new List<User>(); int counter = 0; foreach (ProductDto productDto in deserializer) { if (IsValid(productDto) == false) { Console.WriteLine("Invalid product data"); continue; } var product = mapper.Map <Product>(productDto); int?buyerId = new Random().Next(1, 30); int sellerId = new Random().Next(31, 56); if (counter == 4) { buyerId = null; counter = 0; } counter++; product.BuyerId = buyerId; product.SellerId = sellerId; var context = new XmlProcessingExerciseContext(); context.Add(product); context.SaveChanges(); } Console.WriteLine("Products added successfully!"); }