Exemple #1
0
        private Product MapItem(XmlReader reader)
        {
            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"), CultureInfo.InvariantCulture);
            product.Discontinued = bool.Parse(reader.GetAttribute("discontinued"));
            return product;
        }
Exemple #2
0
        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;
        }
 // La clase es responsable de devolver los productos a partir del nombre de un archivo
 // abrir el archivo p/obtener los datos
 // convertir de xml a objetos
 public IEnumerable<Product> GetByFileName(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();
             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"));
             products.Add(product);
         }
     }
     return products;
 }