Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

ASP.NET Core - XML

$
0
0

Hi,

I need your help again.

Last week I published a thread because I couldn't read the XML.

This I have already managed to do.

But now I have another problem. When I try to read the file it gives this error.(link bellow)

Error

I will put some code lines.

XML:

-<products>


-<product><id>100</id><name>Ball</name><price>15</price><quantity>2</quantity>


-<description><comment>aaa</comment></description></product></products>

MODEL:

 [Table("Table")]
    public class Product
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Price { get; set; }
        public int Quantity { get; set; }

        public Description Description{ get; set; }
        
    }
   
   public class Description
    {
        [Key]
        public string DescriptionComment { get; set; }
    }

CONTROLLER:

  private List<Product> ProcessImport(string path)
        {
            XDocument xDocument = XDocument.Load(path);
            List<Product> products = xDocument.Descendants("product").Select
                (p => new Product()
                {
                   Id = Convert.ToInt32(p.Element("id").Value),
                   Name=p.Element("name").Value,
                   Quantity = Convert.ToInt32(p.Element("quantity").Value),
                   Price = Convert.ToInt32(p.Element("price").Value),
                   Description = new Description()
                   {
                       DescriptionComment= p.Element("description").Element("comment").Value
                   }
                }).ToList();
            foreach(var product in products)
            {
                var productInfo = db.Products.SingleOrDefault(p => p.Id.Equals(product.Id));
                if (productInfo != null)
                {
                    productInfo.Id = product.Id;
                    productInfo.Name = product.Name;
                    productInfo.Quantity = product.Quantity;
                    productInfo.Price = product.Price;
(I think the problem is here!) } else { db.Products.Add(product); } db.SaveChanges(); } return products; }

If you can help me I woul appreciate.

Thanks!

Best regards

Andre


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>