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?
ToDictionaryon theIEnumerablereturned by each of the select queries: msdn.microsoft.com/en-us/library/bb549277%28v=vs.100%29.aspx