public Product Map(XmlReader reader) { if(reader == null) throw new ArgumentNullException("XML reader used when mapping cannot be null."); if(reader.Name != "product") throw new InvalidOperationException("XML reader is not on a product fragment."); var product = new Product(); product.Id = int.Parse(reader.GetAttribute("id")); product.Name = reader.GetAttribute("name"); product.UnitPrice = decimal.Parse(reader.GetAttribute("unitPrice")); product.Discontinued = bool.Parse(reader.GetAttribute("discontinued")); return product; }
public List<Product> ReadProducts(string fileName) { var products = new List<Product>(); using (var fs = new FileStream(fileName, FileMode.Open)) { var reader = XmlReader.Create(fs); while (reader.Read()) { if (reader.Name != "product") { continue; } var product = new Product( reader.GetAttribute("id"), reader.GetAttribute("name"), reader.GetAttribute("unitPrice"), reader.GetAttribute("discontinued")); products.Add(product); } } return products; }