2

I have an XML file setup as follows:

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

And I am using LINQ to XML as follows:

  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;

The following is the product class:

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

The only part I can't figure out how to get working is ResaleDMs and PurchaseDMs.

The select statements are both showing "Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Collections.Generic.Dictionary'. An explicit conversion exists (are you missing a cast?)"

Can anyone help me figure this out? How do I initialize those fields?

1

2 Answers 2

1

The reason you are getting that exception is because you are assigning the dictionary properties the value of an IEnumerable projection.

When you do:

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

You are just creating a new anonymous object type with two properties (key & value). This does not directly correlate to a dictionary entry object type. Instead you could use the ToDictionary extension method..

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))
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I don't know how many variations on this I tried and could not get the correct syntax.
0

I think you should be able to get the IEnumerable by using ToArray():

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.