public static string ImportCategoryProducts(ProductShopContext context, string inputXml)
        {
            var serializer       = new XmlSerializer(typeof(CategoryProduct[]), new XmlRootAttribute("CategoryProducts"));
            var categoryProducts = (CategoryProduct[])serializer.Deserialize(new StringReader(inputXml));

            foreach (var item in categoryProducts)
            {
                if (context.Categories.Find(item.CategoryId) != null && context.Products.Find(item.ProductId) != null)
                {
                    context.Add(item);
                }
            }
            var count = context.SaveChanges();

            return($"Successfully imported {count}");
        }
Esempio n. 2
0
        public static string ImportProducts(ProductShopContext context, string inputXml)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(ImportProductDto[]), new XmlRootAttribute("Products"));

            var products = (ImportProductDto[])serializer.Deserialize(new StringReader(inputXml));


            foreach (var pro in products)
            {
                var product = Mapper.Map <Product>(pro);

                context.Add(product);
            }
            int result = context.SaveChanges();

            return($"Successfully imported {result}");
        }
Esempio n. 3
0
        public static string ImportProducts(ProductShopContext context, string inputXml)
        {
            var productsDto = new List <ImportProductDTO>();
            var serializer  = new XmlSerializer(productsDto.GetType(), new XmlRootAttribute("Products"));

            using (var stream = CreateStreamFromString(inputXml))
            {
                productsDto = (List <ImportProductDTO>)serializer.Deserialize(stream);
            }

            foreach (var productDto in productsDto.Where(p => p.Name != null))
            {
                var product = Mapper.Map <Product>(productDto);
                context.Add(product);
            }

            var count = context.SaveChanges();

            return($"Successfully imported {count}");
        }
Esempio n. 4
0
        public static string ImportUsers(ProductShopContext context, string inputXml)
        {
            var usersDto = new List <ImportUserDTO>();

            var serializer = new XmlSerializer(usersDto.GetType(), new XmlRootAttribute("Users"));

            using (var stream = CreateStreamFromString(inputXml))
            {
                usersDto = (List <ImportUserDTO>)serializer.Deserialize(stream);
            }

            foreach (var userDto in usersDto.Where(u => u.LastName != null))
            {
                var user = Mapper.Map <User>(userDto);
                context.Add(user);
            }

            var count = context.SaveChanges();

            return($"Successfully imported {count}");
        }
Esempio n. 5
0
        public static string ImportUsers(ProductShopContext context, string inputXml)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(ImportUserDto[]), new XmlRootAttribute("Users"));

            var users = (ImportUserDto[])serializer.Deserialize(new StringReader(inputXml));

            foreach (var user in users)
            {
                User userToImport = new User
                {
                    FirstName = user.FirstName,
                    LastName  = user.LastName,
                    Age       = user.Age
                };

                context.Add(userToImport);
            }

            int result = context.SaveChanges();

            return($"Successfully imported {result}");
        }