使用LINQ将带有属性的XML转换为Object

时间:2013-02-23 06:05:51

标签: c# xml linq linq-to-xml

我有一个XML文件设置如下:

<products>
<product>
    <tradegood id = "11">Textiles</tradegood>
    <baseprice>3000</baseprice>
    <purchaseDMs>
        <mod type="A">-7</mod>
        <mod type="a">-5</mod>
        <mod type="i">-3</mod>
    </purchaseDMs>
    <resaleDMs>
        <mod type="A">-6</mod>
        <mod type="a">1</mod>
        <mod type="R">3</mod>
    </resaleDMs>
    <quantity>90</quantity>
</product>

我正在使用LINQ to XML,如下所示:

  XDocument productList = XDocument.Load("products.xml");

  List<Product> products = 
                (from objProduct in productList.Element("products").Elements("product")
                    select new Product
                    {
                         Id = int.Parse(objProduct.Element("tradegood").Attribute("id").Value),
                         ProductName = objProduct.Element("tradegood").Value,
                         BasePrice = double.Parse(objProduct.Element("baseprice").Value),
                         MaxQuantity = int.Parse(objProduct.Element("quantity").Value),
                         PurchaseDMs = (from _mods in objProduct.Element("purchaseDMs").Elements("mod")
                              select new 
                                {
                                key = _mods.Attribute("type").Value,
                                value = _mods.Value
                                }),
                         ResaleDMs = (from _mods in objProduct.Element("resaleDMs").Elements("mod")
                              select new 
                                {
                                key = _mods.Attribute("type").Value,
                                value = _mods.Value
                                })
                      }).ToList;

以下是产品类:

public class Product
{
    private string p_ProductName;
    private double p_BasePrice;
    private int p_MaxQuantity;
    private double p_ActualValue;
    private int p_id;
    private int p_Quantity;

    public string ProductName
    {
        get { return p_ProductName; }
        set { p_ProductName = value; }
    }
    public double BasePrice
    {
        get { return p_BasePrice; }
        set { p_BasePrice = value; }
    }
    public int MaxQuantity
    {
        get { return p_MaxQuantity; }
        set { p_MaxQuantity = value; }
    }
    public int QuantityAvailable 
    {
        get { return p_Quantity; }
        set { p_Quantity = value; }
    }
    public double ActualValue
    {
        get { return p_ActualValue; }
        set { p_ActualValue = value; }
    }
    public int Id
    {
        get { return p_id; }
        set { p_id = value; }
    }

    public Dictionary<string, int> ResaleDMs;
    public Dictionary<string, int> PurchaseDMs;
}

我唯一能解决如何工作的部分是ResaleDM和PurchaseDMs。

select语句都显示&#34;无法隐式转换类型&#39; System.Collections.Generic.IEnumerable&#39;到&#39; System.Collections.Generic.Dictionary&#39;。存在显式转换(您是否错过了演员?)&#34;

任何人都可以帮我解决这个问题吗?如何初始化这些字段?

2 个答案:

答案 0 :(得分:1)

您获得该异常的原因是因为您正在为字典属性分配IEnumerable投影的值。

当你这样做时:

select new 
{
  key = _mods.Attribute("type").Value,
  value = _mods.Value
}

您只是创建一个具有两个属性(键和值)的新匿名对象类型。这与字典条目对象类型不直接相关。相反,你可以使用ToDictionary扩展方法..

PurchaseDMs = objProduct.Element("purchaseDMs").Elements("mod")
                        .ToDictionary(e => e.Attribute("type").Value, e => Convert.ToInt32(e.Value)),
ResaleDMs = objProduct.Element("resaleDMs").Elements("mod")
                      .ToDictionary(e => e.Attribute("type").Value, e => Convert.ToInt32(e.Value))

答案 1 :(得分:0)

我认为您应该能够通过使用ToArray()来获取IEnumerable:

List<Product> products = 
            (from objProduct in productList.Element("products").Elements("product")
                select new Product
                {
                     Id = int.Parse(objProduct.Element("tradegood").Attribute("ID").Value),
                     ProductName = objProduct.Element("tradegood").Value,
                     BasePrice = double.Parse(objProduct.Element("baseprice").Value),
                     MaxQuantity = int.Parse(objProduct.Element("quantity").Value),
                     PurchaseDMs = (from _mods in objProduct.Element("purchaseDMs").Elements("mod")
                          select new 
                            {
                            key = _mods.Attribute("type").Value,
                            value = _mods.Value
                            }),
                     ResaleDMs = (from _mods in objProduct.Element("resaleDMs").Elements("mod")
                          select new 
                            {
                            key = _mods.Attribute("type").Value,
                            value = _mods.Value
                            })
                  }).ToArray();