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(),
};
答案 0 :(得分:1)
用类似的东西改变你的最后一行(没有你的xml和类结构很难做得更好)。
CustList = results.Descendants("Customer")
.Select(m => new Customer {
LastName = m.Element("LastName").Value,
FirstName = m.Element("FirstName").Value,
}).ToList();
答案 1 :(得分:1)
将投射元素用于string
,int
或DateTime
,而不是获取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()
};