簡體   English   中英

Asp.Net Core - 讀取文件

[英]Asp.Net Core - Read Files

我再次需要你的幫助。 我一直在 asp.net 從事一個項目,我有一些疑問。

該項目包括讀取 xml 文件並將數據保存在數據庫中。 我能夠讀取數據,但現在我有一個問題問題是......

我有這個 xml 文件。

 -<products> -<product> <name>Ball</name> <price>15</price> <quantity>2</quantity> -<description> <comment>aaa</comment> </description> -<description> <comment>bbb</comment> </description> -<age> <number>12</number> </age> </product> </products>

這就是程序。

 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 = p.Element("description").Element("comment")


            }).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;
                productInfo.Description= product.Description;


            }

            else 
            {


                db.Products.Add(product);
            }

            db.SaveChanges();
        }

        return products;


    }

我的問題是,上面的程序只讀取第一個描述。

我必須插入什么才能讓他閱讀這兩個描述? (我認為理想的做法是創建兩個表,然后在它們之間建立連接)

但現在我需要幫助來完成這項工作。

謝謝!!

請注意,您的 xml 文件不包含id節點,但您需要在ProcessImport方法中讀取id節點數據。如果不包含id節點,則無法讀取代碼中的數據。

根據您的要求,這是一個工作演示:

Model:

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; }
}

Controller:

private List<Product> ProcessImport()
{
    XDocument xDocument = XDocument.Load("test.xml");
    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 = p.Descendants("description").Select(d => new Description()
            {
                Comment = d.Element("comment").Value
            }).ToArray()


        }).ToList();

    //do your stuff...
    return products;
}

結果: 在此處輸入圖像描述 我的測試。xml:

<?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>
    <description>
      <comment>bbb</comment>
    </description>
    <age>
      <number>12</number>
    </age>
  </product>
</products>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM