Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. public IEnumerable<Book> GetAllProducts()
  2. {
  3.  
  4. XDocument doc = XDocument.Load("C:\Users\Name\Desktop\products.xml");
  5.  
  6. foreach (XElement element in doc.Descendants("Catalog")
  7.  
  8. .Descendants("Product"))
  9.  
  10. {
  11.  
  12. Product product = new Product();
  13.  
  14. product.Id = element.Element("Id").Value;
  15. product.Name = element.Element("Name").Value;
  16. product.Category = element.Element("Category").Value;
  17. product.Added_Date = element.Element("Added_Date").Value;//this will not work because its not a string
  18. product.Price = element.Element("Price").Value;//this will not work because its not a string
  19.  
  20. products.Add(product);
  21.  
  22. }
  23.  
  24. return products;
  25. }
  26.  
  27. <?xml version="1.0"?>
  28. <catalog>
  29. <product id="P1">
  30. <name>Royal Gala</name>
  31. <category>Apple</category>
  32. <country>New Zeeland</country>
  33. <price>33.00</price>
  34. <added_date>2011-01-11</added_date>
  35. <description>This is a lovely red apple.</description>
  36. </product>
  37. <product id="P2">
  38. <name>Granny Smith</name>
  39. <category>Apple</category>
  40. <country>Australia</country>
  41. <price>33.00</price>
  42. <added_date>2013-12-25</added_date>
  43. <description>This is a lovely green apple.</description>
  44. </product>
  45. </catalog>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement