Пример #1
0
        public void ExportUsersAndProducts()
        {
            UsersColletionDto usersColletion = new UsersColletionDto
            {
                Count = this.Context.Users.Count(),
                Users = this.Context.Users
                        .Where(u => u.ProductsSold.Count > 0)
                        .Select(u => new UserDto
                {
                    FirstName    = u.FirstName,
                    LastName     = u.LastName,
                    Age          = u.Age.ToString(),
                    SoldProducts = new SoldProductsCollectionDto
                    {
                        Count       = u.ProductsSold.Count,
                        SoldProduct = u.ProductsSold.Select(p => new SoldProductNamePriceDto
                        {
                            Name  = p.Name,
                            Price = p.Price,
                        }).ToArray()
                    }
                }).ToArray()
            };

            JsonSerializerSettings settings = new JsonSerializerSettings
            {
                DefaultValueHandling = DefaultValueHandling.Ignore,
            };

            string jsonString = JsonConvert.SerializeObject(usersColletion, Formatting.Indented, settings);

            File.WriteAllText(pathToJsonsFolder + "users-and-products.json", jsonString);
        }
        public void ExportUsersAndProducts()
        {
            UsersColletionDto users = new UsersColletionDto
            {
                Count = this.Context.Users.Count(),
                Users = this.Context.Users
                        .Where(u => u.ProductsSold.Count > 0)
                        .Select(u => new UserDto
                {
                    FirstName   = u.FirstName,
                    LastName    = u.LastName,
                    Age         = u.Age.ToString(),
                    SoldProduct = new SoldProductsCollectionDto
                    {
                        Count        = u.ProductsSold.Count(),
                        SoldProducts = u.ProductsSold.Select(p => new SoldProductsAttributesDto
                        {
                            Name  = p.Name,
                            Price = p.Price
                        }).ToArray()
                    }
                }).ToArray()
            };

            StringBuilder           builder       = new StringBuilder();
            XmlSerializerNamespaces xmlNamespaces = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty });

            XmlSerializer serializer = new XmlSerializer(typeof(UsersColletionDto), new XmlRootAttribute("users"));

            serializer.Serialize(new StringWriter(builder), users, xmlNamespaces);

            File.WriteAllText("../../../Xmls/users-and-products.xml", builder.ToString());
        }