public static void BuildXml() { XmlNode rootNode = doc.CreateElement("Recipe"); var allText = File.ReadAllText(@"C:\Users\abrhm\source\repos\RecipeReader\RecipeReader\1_LemonCake.txt"); NodeRepresentation header = new NodeRepresentation("Header", @"\d\.", null, ReadingRule.HEADER); NodeRepresentation[] ingredients = { new NodeRepresentation("Ingredient", @"\n", null) }; NodeRepresentation[] steps = { new NodeRepresentation("Step", @"\d\.", null) }; NodeRepresentation[] categories = { header, new NodeRepresentation("Ingredients", "Ingredients:", ingredients), new NodeRepresentation("Method", "Method:", steps) }; BuildNode(rootNode, categories, allText); doc.AppendChild(rootNode); doc.Save(@"C:\Users\abrhm\source\repos\RecipeReader\RecipeReader\EncodedRecipe.xml"); }
private static void BuildShallowNode(string nodeText, NodeRepresentation shallowNodeRepresentation, XmlNode parentNode) { var words = Regex.Split(nodeText, shallowNodeRepresentation.StartSeparator); foreach (var word in words) { XmlNode shallowNode = doc.CreateElement(shallowNodeRepresentation.XmlName); if (string.IsNullOrWhiteSpace(word)) { continue; } shallowNode.InnerText = word.Trim(); parentNode.AppendChild(shallowNode); } }
private static void BuildHeaderNode(string text, NodeRepresentation nodeRep, XmlNode parentNode) { var pattern = @"(\d)\.(.+)\[(.+)\].+"; var match = Regex.Match(text, pattern); var recipeNumber = match.Groups[1].Value; var recipeTitle = match.Groups[2].Value.Trim(); var datePosted = match.Groups[3].Value; XmlNode newNode = doc.CreateElement(nodeRep.XmlName); XmlNode numberNode = doc.CreateElement("RecipeNumber"); numberNode.InnerText = recipeNumber; XmlNode titleNode = doc.CreateElement("Title"); titleNode.InnerText = recipeTitle; XmlNode dateNode = doc.CreateElement("Date"); dateNode.InnerText = datePosted; newNode.AppendChild(numberNode); newNode.AppendChild(titleNode); newNode.AppendChild(dateNode); parentNode.AppendChild(newNode); }