internal void ProcessIterativeMarkup() { StringBuilder repeatText = new StringBuilder(); ContentFinder finder = new ContentFinder(CompiledDoc); if (DocumentElement.Element("ITERATIONS") != null) { var totalIterations = Convert.ToInt32(DocumentElement.Element("ITERATIONS").Descendants().Count()); List <int> iterationCounts = new List <int>(); for (int j = 0; j < totalIterations; j++) { iterationCounts.Add(Convert.ToInt32(DocumentElement.Element("ITERATIONS").Elements("ITERATION").ToArray <XElement>()[j].Value)); } for (int j = 0; j < totalIterations; j++) { string startTerm = string.Empty; string endTerm = string.Empty; if (j == 0) { startTerm = CompiledMarkupConstants._FOREACH_BEGIN; endTerm = CompiledMarkupConstants._FOREACH_END; } else { startTerm = string.Format("{0}-{1}", CompiledMarkupConstants._FOREACH_BEGIN, j); endTerm = string.Format("{0}{1}>>", CompiledMarkupConstants._FOREACH_END_MULTIPLE, j); } var foundForEachRangeContent = finder.FindMatchingNodes(startTerm, endTerm); if (foundForEachRangeContent.Count > 0) { foreach (NodeRangeMatch match in foundForEachRangeContent) { // Process the Iterations in *match* order var iterationCount = iterationCounts.First(); iterationCounts.RemoveAt(0); bool beginTagInTable = false; Node begin = match.StartNode; Node end = match.EndNode; ReplaceMarkupWithField(begin, startTerm); ReplaceMarkupWithField(end, endTerm); // Copy the nodes *including the start term that is now a dummy field tag* to match // the existing expansion List <Node> extractedNodes = ContentExtractor.ExtractContent(begin, end, true, false, true); Node insertAfter = begin.GetAncestor(NodeType.Paragraph); beginTagInTable = IsTagInTable(begin); // Expand the Foreach section for the number of iterations extractedNodes.Reverse(); for (var i = 0; i < iterationCount - 1; i++) { insertAfter = AddRowForStartIteration(begin, insertAfter); foreach (Node insertNode in extractedNodes) { if (insertNode.NodeType == NodeType.Table && beginTagInTable) { // Insert the *contents* of the table rather than the table as a whole InsertTableContents(insertNode, insertAfter); } else { insertAfter.ParentNode.InsertAfter(insertNode.Clone(true), insertAfter); } } } } } } } }