public static string BuildHtml(string html, KeyValueVariables keyValue, KeyTableVariables keyTable) { List <Node> nodeList = AnalyseTemplate(html); Stack <Node> stack = new Stack <Node>(nodeList); while (stack.Count > 0) { Node node = stack.Pop(); if (node.PlaceholdType == PlaceholdType.Table) { html = html.Replace(node.OuterHtml, node.UniqueSign); } else { string id = node.ID; bool show; if (keyValue.ContainsKey(id) && keyValue[id] != null && bool.TryParse(keyValue[id].ToString(), out show) && show) { foreach (var n in node.Children) { stack.Push(n); } nodeList.AddRange(node.Children); html = html.Replace(node.OuterHtml, node.InnerHtml); } else { html = html.Replace(node.OuterHtml, string.Empty); } nodeList.Remove(node); } } // 处理KeyValue MatchCollection matchCollectioin = Regex.Matches(html, OUTPUT_PLACE_HOLDER, RegexOptions.IgnoreCase | RegexOptions.Multiline); foreach (Match matchField in matchCollectioin) { if (matchField.Success) { if (matchField.Groups["id"] == null || string.IsNullOrWhiteSpace(matchField.Groups["id"].Value)) { throw new ApplicationException("no id"); } string id = matchField.Groups["id"].Value.Trim(); if (keyValue.ContainsKey(id)) { html = html.Replace(matchField.Groups[0].Value, CovertToString(keyValue[id])); } else { html = html.Replace(matchField.Groups[0].Value, string.Empty); } } } // 处理循环 foreach (Node node in nodeList) { if (keyTable.ContainsKey(node.ID) && keyTable[node.ID] != null) { html = html.Replace(node.UniqueSign, node.BuildHtml(keyTable[node.ID])); } else { html = html.Replace(node.UniqueSign, string.Empty); } } return(html); }