Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static void GetProductsInRangeXml()
- {
- using (var db = new ProductsShopContext())
- {
- var products = db.Products
- .Where(p => p.Price >= 1000 && p.Price <= 2000 && p.BuyerId != null)
- .OrderBy(p => p.Price)
- .Select(p => new
- {
- productName = p.Name,
- price = p.Price,
- buyer = $"{p.Buyer.FirstName} {p.Buyer.LastName}"
- }).ToArray();
- var xDoc = new XDocument();
- xDoc.Add(new XElement("products"));
- //output file - formating: one line (same as sample output)
- foreach (var p in products)
- {
- xDoc.Root.Add(
- new XElement("product",
- new XAttribute("name", $"{p.productName}"),
- new XAttribute("price", $"{p.price}"),
- new XAttribute("buyer", $"{p.buyer}")));
- }
- //output file - formating: multiline
- //foreach (var p in products)
- //{
- // xDoc.Root.Add(
- // new XElement("product",
- // new XElement("name", $"{p.productName}"),
- // new XElement("price", $"{p.price}"),
- // new XElement("buyer", $"{p.buyer}")));
- //}
- xDoc.Save("OutputFiles/ProductsInRange.xml");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement