I have the following C# code and I have no idea why it's not working (I'm getting a NullReferenceException error). If I define Recipe as new List() everything starts working OK.
foreach (XElement element in document.Descendants("vegetables"))
{
VegetablesList = (
from vegetables in element.Elements()
select new FoodItem()
{
Name = (vegetables.Element("name") == null) ? null : vegetables.Element("name").Value.ToString(),
Bcg = (vegetables.Element("bcg") == null) ? null : vegetables.Element("bcg").Value.ToString(),
Info = (vegetables.Element("info") == null) ? null : vegetables.Element("info").Value.ToString(),
Recipes = (
from recipes in element.Element("recipes").Elements()
select new Recipe()
{
Name = (recipes.Element("name") == null) ? null : recipes.Element("name").Value.ToString(),
Text = (recipes.Element("text") == null) ? null : recipes.Element("text").Value.ToString()
}
).ToList()
}
).ToList();
VegetablesListBox.ItemsSource = VegetablesList;
}
Thanks for your help!