// Construction du corpus XML via StringBuilder (evite le piege du verbatim string @"...",
// ou chaque guillemet XML devrait etre double sous peine de CS1003/CS1010).
var sb = new StringBuilder();
sb.Append("<recipeml>");
Action<string, string, int, int, int, int, int, int, string, int> add =
(name, cat, kcal, prot, carbs, fat, cost, prep, allerg, idx) =>
{
sb.Append($"<recipe><head><title>{name}</title><category>{cat}</category></head>");
sb.Append($"<nutrition kcal=\"{kcal}\" protein=\"{prot}\" carbs=\"{carbs}\" fat=\"{fat}\"/>");
sb.Append($"<prep time=\"{prep}\"/>");
sb.Append($"<ingredients>(corpus inline)</ingredients>");
sb.Append($"<allergens>{allerg}</allergens>");
sb.Append($"<cost currency=\"EUR\">{cost}</cost></recipe>");
};
add("Salade de quinoa", "entree", 180, 6, 24, 6, 280, 15, "none", 0);
add("Veloute de potiron", "entree", 120, 4, 18, 3, 220, 20, "lactose", 1);
add("Poulet roti aux herbes","plat", 520, 45, 8, 32, 850, 60, "none", 2);
add("Lasagnes bolognaise", "plat", 610, 28, 55, 30, 720, 75, "gluten,lactose", 3);
add("Tofu curry coco", "plat", 430, 22, 30, 24, 540, 35, "none", 4);
add("Brownie aux noix", "dessert", 380, 6, 42, 22, 410, 45, "gluten,lactose,nuts", 5);
add("Salade de fruits frais","dessert", 90, 1, 22, 0, 180, 10, "none", 6);
sb.Append("</recipeml>");
string recipesXml = sb.ToString();
public class Recipe
{
public string Name { get; set; }
public string Category { get; set; }
public int Kcal { get; set; }
public int Protein { get; set; }
public int Carbs { get; set; }
public int Fat { get; set; }
public int CostCents { get; set; }
public int PrepMin { get; set; }
public string Allergens { get; set; }
public bool HasAllergen(string a) => Allergens != "none" && Allergens.Split(',').Contains(a);
}
var doc = XDocument.Parse(recipesXml);
var corpus = doc.Root.Elements("recipe").Select(r => new Recipe
{
Name = (string)r.Element("head").Element("title"),
Category = (string)r.Element("head").Element("category"),
Kcal = (int)r.Element("nutrition").Attribute("kcal"),
Protein = (int)r.Element("nutrition").Attribute("protein"),
Carbs = (int)r.Element("nutrition").Attribute("carbs"),
Fat = (int)r.Element("nutrition").Attribute("fat"),
CostCents = (int)r.Element("cost"),
PrepMin = (int)r.Element("prep").Attribute("time"),
Allergens = (string)r.Element("allergens") ?? "none"
}).ToList();
Console.WriteLine("Corpus parse : " + corpus.Count + " recettes");
Console.WriteLine(string.Format("{0,-24} {1,-8} {2,4} {3,4} {4,6} {5,-18}", "Nom", "Cat", "kcal", "prot", "prix", "allergenes"));
foreach (var rc in corpus)
Console.WriteLine(string.Format("{0,-24} {1,-8} {2,4} {3,4} {4,4} c {5,-18}", rc.Name, rc.Category, rc.Kcal, rc.Protein, rc.CostCents, rc.Allergens));