using System; using System.Collections.Generic; using System.Text; using System.Xml; using System.IO; namespace Cooking { public class Recipe { /// /// Path to the xml file we will be reading/writing to. /// public static string RecipePath = @"Docs\Recipes.xml"; /// /// The XML document we will be using. /// public static XmlDocument RecipeDoc = new XmlDocument(); /// /// The local collection of recipes we will be using/building. /// public static List m_Recipes; /// /// The name of the recipe. /// private string m_Name; public string Name { get { return m_Name; } set { m_Name = value; } } /// /// User added notes about the recipe. /// private string m_Notes; public string Notes { get { return m_Notes; } set { m_Notes = value; } } /// /// The recipes collection of bases. /// private List m_Bases; public List Bases { get { return m_Bases; } set { m_Bases = value; } } /// /// The recipes collection of additives. /// private List m_Additives; public List Additives { get { return m_Additives; } set { m_Additives = value; } } /// /// The entire collection of ingredients, includes all bases and additives. /// private List m_AllIngredients; public Recipe(string name) { Initialize(); m_Name = name; } /// /// Initializes various things( like the bases, additives, and ingredits collections ). /// private void Initialize() { m_Bases = new List(); m_Additives = new List(); m_AllIngredients = new List(); } /// /// Adds a cooking base to the Bases collection and Ingredients collection. /// /// The cooking base to add. public void AddBase(Ingredient b) { m_Bases.Add(b); m_AllIngredients.Add(b); } /// /// Adds a cooking additive to the Additives collection and Ingredients collection. /// /// The cooking additive to add. public void AddAdditive(Ingredient a) { m_Additives.Add(a); m_AllIngredients.Add(a); } /// /// Adds the created recipe to the Recipes collection and then regenerates the xml document from the contents of Recipes. /// /// public static void AddWriteRecipe(Recipe m_Recipe) { m_Recipes.Add(m_Recipe); RecipeDoc = new XmlDocument(); if (File.Exists(RecipePath)) File.Delete(RecipePath); XmlDeclaration Declaration = RecipeDoc.CreateXmlDeclaration("1.0", "utf-8", ""); RecipeDoc.AppendChild(Declaration); XmlNode Recipes = RecipeDoc.CreateNode(XmlNodeType.Element, "Recipes", ""); XmlComment Comment = RecipeDoc.CreateComment("This file was generated by ATITD Companion, written by Sithid( *********** )."); Recipes.AppendChild(Comment); foreach (Recipe r in m_Recipes) { // Create the recipe node. XmlNode RecipeNode = RecipeDoc.CreateNode(XmlNodeType.Element, "Recipe", ""); // Add the notes attribute. XmlAttribute RecipeNotesAttribute = RecipeDoc.CreateAttribute("notes"); // Create the attribute. RecipeNotesAttribute.InnerText = r.Notes; // Change the InnerText. RecipeNode.Attributes.Append(RecipeNotesAttribute); // Add the attribute to the main nodes attributes collection. // Add the name node. XmlNode RecipeNameNode = RecipeDoc.CreateNode(XmlNodeType.Element, "Name", ""); // Create the node. RecipeNameNode.InnerText = r.Name; // Change the InnerText. RecipeNode.AppendChild(RecipeNameNode); // Add the node as a child to the main recipe node. // Add the ingredients node. XmlNode RecipeIngredientsNode = RecipeDoc.CreateNode(XmlNodeType.Element, "Ingredients", ""); // Create the ingredients node. // Add the bases node. XmlNode AllBasesNode = RecipeDoc.CreateNode(XmlNodeType.Element, "Bases", ""); // Loop through the recipes Bases collection. foreach (Ingredient i in r.Bases) { XmlNode BaseNode = RecipeDoc.CreateNode(XmlNodeType.Element, "Base", ""); XmlNode BaseNameNode = RecipeDoc.CreateNode(XmlNodeType.Element, "Name", ""); BaseNameNode.InnerText = i.Name; BaseNode.AppendChild(BaseNameNode); XmlNode BaseAmountNode = RecipeDoc.CreateNode(XmlNodeType.Element, "Amount", ""); BaseAmountNode.InnerText = i.Amount.ToString(); BaseNode.AppendChild(BaseAmountNode); AllBasesNode.AppendChild(BaseNode); } RecipeIngredientsNode.AppendChild(AllBasesNode); // Add the additives node. XmlNode AllAdditivesNode = RecipeDoc.CreateNode(XmlNodeType.Element, "Additives", ""); foreach (Ingredient i in r.Additives) { XmlNode AdditiveNode = RecipeDoc.CreateNode(XmlNodeType.Element, "Additive", ""); XmlNode AdditiveNameNode = RecipeDoc.CreateNode(XmlNodeType.Element, "Name", ""); AdditiveNameNode.InnerText = i.Name; AdditiveNode.AppendChild(AdditiveNameNode); XmlNode AdditiveAmountNode = RecipeDoc.CreateNode(XmlNodeType.Element, "Amount", ""); AdditiveAmountNode.InnerText = i.Amount.ToString(); AdditiveNode.AppendChild(AdditiveAmountNode); AllAdditivesNode.AppendChild(AdditiveNode); } RecipeIngredientsNode.AppendChild(AllAdditivesNode); RecipeNode.AppendChild(RecipeIngredientsNode); Recipes.AppendChild(RecipeNode); } RecipeDoc.AppendChild(Recipes); RecipeDoc.Save(RecipePath); LoadRecipes(); } public static List LoadRecipes() { List list = new List(); if (File.Exists(RecipePath)) RecipeDoc.Load(RecipePath); else { m_Recipes = new List(); return new List(); } XmlNodeList Nodes = RecipeDoc.SelectNodes("Recipes/Recipe"); foreach (XmlNode RecipeNode in Nodes) { Recipe r = new Recipe("generic_name"); r.Notes = RecipeNode.Attributes["notes"].Value; r.Name = RecipeNode.SelectSingleNode("Name").InnerText; XmlNodeList Ingredients = RecipeNode.SelectNodes("Ingredients"); foreach (XmlNode n in Ingredients) { XmlNodeList Bases = n.SelectNodes("Bases/Base"); XmlNodeList Additives = n.SelectNodes("Additives/Additive"); string TempName = string.Empty; int TempAmount = 0; foreach (XmlNode bn in Bases) { try { TempName = bn.SelectSingleNode("Name").InnerText; if (TempName == "") TempName = "empty"; } catch { } try { if (!int.TryParse(bn.SelectSingleNode("Amount").InnerText, out TempAmount)) TempAmount = 0; } catch { } r.AddBase(new Ingredient(TempName, TempAmount)); } foreach (XmlNode an in Additives) { try { TempName = an.SelectSingleNode("Name").InnerText; if (TempName == "") TempName = "string.Empty"; } catch { } try { if (!int.TryParse(an.SelectSingleNode("Amount").InnerText, out TempAmount)) TempAmount = 0; } catch { } r.AddAdditive(new Ingredient(TempName, TempAmount)); } } list.Add(r); } m_Recipes = list; return list; } public override string ToString() { return m_Name; } } }