private static void LoadFoods(string dataFile) { Categories = new Dictionary<int, Category>(); Foods = new Dictionary<int, Food>(); var root = XElement.Load(dataFile); int catId = 0; int foodId = 0; foreach (var cat in root.Elements("category")) { var category = new Category() { Id = catId++, Name = (string)cat.Attribute("Name"), }; foreach (var f in cat.Descendants("foods").Elements("food")) { var food = new Food() { Id = foodId++, Category = category, Name = (string) f.Attribute("Name"), GlycemicIndex = (int) f.Attribute("GI"), GlycemicLoad = (int) f.Attribute("GL"), GIComplete = (string)f.Attribute("GIFull"), ServingSize = (string)f.Attribute("Serving") }; category.Foods.Add(food); Foods.Add(food.Id, food); AddFoodToAppropriateIndexRange(food); } Categories.Add(category.Id, category); } }
private static void AddFoodToAppropriateIndexRange(Food food) { if( food.GlycemicIndex < IndexRanges[IndexLevel.Med].MinValue ) IndexRanges[IndexLevel.Low].Foods.Add(food); else if( food.GlycemicIndex > IndexRanges[IndexLevel.Med].MaxValue ) IndexRanges[IndexLevel.High].Foods.Add(food); else IndexRanges[IndexLevel.Med].Foods.Add(food); }