locked
Read XML File in ASP.NET CORE MVC RRS feed

  • Question

  • User-1925037784 posted

    Hi,

    I am working on a project where i have to read an xml file.

    But I have a problem. 

    <?xml version="1.0" encoding="utf-8" ?>
    <products>
    <product>
        <id>100</id>
        <name>Ball</name>
        <price>15</price>
        <quantity>2</quantity>
    <description>
        <comment>aaa</comment>
    </description>
    </product>
    </products>

    This is an example of the file that I have to import and read.

    I can read id, name, price and quantity. I cant read description.

    Bellow i will show my code in c#.

    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.ToDecimal(p.Element("price").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;
    
    
                }
                else
                {
                    db.Products.Add(product);
                }
    
                db.SaveChanges();
            }
            return products;
        }

    Do I have to do anything different?

    Can you help me!?!?!?

    Thanks!!

    Monday, May 11, 2020 3:25 PM

Answers

  • User-943250815 posted

    You have to navigate up to inner element. For description use

    Description = p.Element("description").Element("comment").Value

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 11, 2020 9:24 PM
  • User711641945 posted

    Hi andre5ilva,

    Description = p.Element("description").Element("comment").Value

    Based on jzero's answer,the whole correct answer should be:

    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.ToDecimal(p.Element("price").Value),
                Description = new Description()
                {
                    Comment= p.Element("description").Element("comment").Value
                }
    
            }).ToList();

    Be sure your model is like below:

    public class Product
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Quantity { get; set; }
        public decimal Price { get; set; }
        public Description Description { get; set; }
    }
    public class Description
    {
        public string Comment { get; set; }
    }

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 12, 2020 2:01 AM

All replies

  • User-943250815 posted

    You have to navigate up to inner element. For description use

    Description = p.Element("description").Element("comment").Value

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 11, 2020 9:24 PM
  • User711641945 posted

    Hi andre5ilva,

    Description = p.Element("description").Element("comment").Value

    Based on jzero's answer,the whole correct answer should be:

    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.ToDecimal(p.Element("price").Value),
                Description = new Description()
                {
                    Comment= p.Element("description").Element("comment").Value
                }
    
            }).ToList();

    Be sure your model is like below:

    public class Product
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Quantity { get; set; }
        public decimal Price { get; set; }
        public Description Description { get; set; }
    }
    public class Description
    {
        public string Comment { get; set; }
    }

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 12, 2020 2:01 AM