public static List <Food> read() { List <Food> data = new List <Food>(); XDocument xDocument = XDocument.Load("../../Db/DB.xml"); IEnumerable <XElement> foods = xDocument.Root.Elements(); foreach (var foodEl in foods) { var foodItem = new Food(); foodItem.Id = foodEl.Element("id").Value; foodItem.Name = foodEl.Element("name").Value; foodItem.Description = foodEl.Element("description").Value; foodItem.ThumbnailPath = foodEl.Element("thumbnailPath").Value; foodItem.IsFavorite = Boolean.Parse(foodEl.Element("isFavorite").Value); foreach (var fStep in foodEl.Element("steps").Elements()) { var stepItem = new FoodStep(); stepItem.StepName = fStep.Element("stepname").Value; stepItem.DescriptionStep = fStep.Element("stepdescription").Value; foreach (var fImg in fStep.Element("stepimagepaths").Elements()) { stepItem.ImageStepPath.Add(fImg.Value); } foodItem.Steps.Add(stepItem); } data.Add(foodItem); } return(data); }
public static Food getById(string id) { dynamic result = null; XDocument xdocument = XDocument.Load("../../Db/DB.xml"); IEnumerable <XElement> foods = xdocument.Root.Elements(); foreach (var foodEl in foods) { if (id == foodEl.Element("id").Value) { var findedFood = new Food(); findedFood.Id = foodEl.Element("id").Value; findedFood.Name = foodEl.Element("name").Value; findedFood.Description = foodEl.Element("description").Value; findedFood.ThumbnailPath = foodEl.Element("thumbnailPath").Value; findedFood.VideoLink = foodEl.Element("video").Value; findedFood.IsFavorite = Boolean.Parse(foodEl.Element("isFavorite").Value); var steps = foodEl.Element("steps").Elements(); foreach (var stepEl in steps) { FoodStep fs = new FoodStep(); fs.DescriptionStep = stepEl.Element("stepdescription").Value; fs.StepName = stepEl.Element("stepname")?.Value; foreach (var img in stepEl.Element("stepimagepaths").Elements()) { fs.ImageStepPath.Add(img.Value); } findedFood.Steps.Add(fs); } result = findedFood; break; } } return(result); }