0

我有一个 xml。xml 有采购订单列表。在该采购订单中我有客户列表。所以通过下面的代码我得到了除客户列表之外的所有元素。我如何将该列表从 xml 分配到我在类中的列表。任何人都可以对其进行排序出去。

 var ItemDet = from results in doc1.Descendants("PurchaseOrderClass")
                              select new PurchaseOrderClass
                              {
                                  Vendor_Name = results.Element("Vendor_Name").Value.ToString(),
                                  ItemCode = results.Descendants("ItemCode").First().Value.ToString(),
                                  Quantity = Int32.Parse(results.Descendants("Quantity").First().Value),
                                  Price = Int32.Parse(results.Element("Price").Value),
                                  Discount = Int32.Parse(results.Descendants("Discount").First().Value),
                                  TotalAmount = Int32.Parse(results.Descendants("TotalAmount").First().Value),
                                  DeliveryDate = Convert.ToDateTime(results.Descendants("DeliveryDate").First().Value),
                                  PO_Number = Int32.Parse(results.Descendants("PO_Number").First().Value),
                                  //CusList= List<Customer> (results.Descendants("Customer")).ToList(),

                              };
4

2 回答 2

1

用类似的方式改变你的最后一行(如果没有你的 xml 和类结构,很难做得更好)。

CustList = results.Descendants("Customer")
                  .Select(m => new Customer {
                       LastName = m.Element("LastName").Value,
                       FirstName = m.Element("FirstName").Value,
                   }).ToList();
于 2013-03-06T13:22:18.883 回答
1

使用强制转换元素stringintDateTime代替获取Value属性。您的查询应如下所示:

 var ItemDet = 
       from o in doc1.Descendants("PurchaseOrderClass")
       select new PurchaseOrderClass
       {
            Vendor_Name = (string)o.Element("Vendor_Name"),
            ItemCode = (string)o.Element("ItemCode"),
            Quantity = (int)o.Element("Quantity"),
            Price = (int)o.Element("Price"),
            Discount = (int)o.Element("Discount"),
            TotalAmount = (int)o.Element("TotalAmount"),
            DeliveryDate = (DateTime)o.Element("DeliveryDate"),
            PO_Number = (int)o.Element("PO_Number"),
            CusList= o.Descendants("Customer")
                      .Select(c => new Customer {
                          // parse properties like
                          // Name = (string)c.Element("Name")
                      }).ToList()
       };
于 2013-03-06T13:29:59.543 回答