Advertisement
Danny_Berova

1.ProductsInRangeXml

Dec 4th, 2017
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.61 KB | None | 0 0
  1. static void GetProductsInRangeXml()
  2.         {
  3.             using (var db = new ProductsShopContext())
  4.             {
  5.                 var products = db.Products
  6.                     .Where(p => p.Price >= 1000 && p.Price <= 2000 && p.BuyerId != null)
  7.                     .OrderBy(p => p.Price)
  8.                     .Select(p => new
  9.                     {
  10.                         productName = p.Name,
  11.                         price = p.Price,
  12.                         buyer = $"{p.Buyer.FirstName} {p.Buyer.LastName}"
  13.                     }).ToArray();
  14.  
  15.                 var xDoc = new XDocument();
  16.                 xDoc.Add(new XElement("products"));
  17.  
  18.                 //output file - formating: one line (same as sample output)
  19.                 foreach (var p in products)
  20.                 {
  21.                     xDoc.Root.Add(
  22.                         new XElement("product",
  23.                             new XAttribute("name", $"{p.productName}"),
  24.                             new XAttribute("price", $"{p.price}"),
  25.                             new XAttribute("buyer", $"{p.buyer}")));
  26.                 }
  27.  
  28.                 //output file - formating: multiline
  29.                 //foreach (var p in products)
  30.                 //{
  31.                 //    xDoc.Root.Add(
  32.                 //        new XElement("product",
  33.                 //            new XElement("name", $"{p.productName}"),
  34.                 //            new XElement("price", $"{p.price}"),
  35.                 //            new XElement("buyer", $"{p.buyer}")));
  36.                 //}
  37.  
  38.                 xDoc.Save("OutputFiles/ProductsInRange.xml");
  39.             }
  40.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement