public ListDefinition(XmlElement xmlDefinition)
        {
            name = xmlDefinition.GetAttribute("Name");
            isReverseOrder = xmlDefinition.GetAttribute("Order").ToLower() == "reverse";

            XmlNode node = xmlDefinition.SelectSingleNode("./ItemTemplate[string-length(@Name) > 0]");
            if (node != null)
            {
                itemTemplate = TemplateRegistry.Templates[((XmlElement)node).GetAttribute("Name")];
                node = null;
            }

            node = xmlDefinition.SelectSingleNode("./AlternatingItemTemplate[string-length(@Name) > 0]");
            if (node != null)
            {
                alternatingItemTemplate = TemplateRegistry.Templates[((XmlElement)node).GetAttribute("Name")];
                node = null;
            }

            node = xmlDefinition.SelectSingleNode("./DividerTemplate[string-length(@Name) > 0]");
            if (node != null)
            {
                alternatingItemTemplate = TemplateRegistry.Templates[((XmlElement)node).GetAttribute("Name")];
                node = null;
            }

            XmlNodeList nodes = xmlDefinition.SelectNodes("./SingleItemTemplate[string-length(@Name) > 0 and @Index > 0]");
            if (nodes.Count > 0)
            {
                singleItemTemplate = new Dictionary<int, Template>();
                foreach (XmlNode snode in nodes)
                {
                    Template t = TemplateRegistry.Templates[((XmlElement)snode).GetAttribute("Name")];
                    int n = int.Parse(((XmlElement)snode).GetAttribute("Index"));
                    singleItemTemplate[n] = t;
                }
            }

            node = xmlDefinition.SelectSingleNode("./PageSelectExpression[1]");
            if (node != null)
                if (node.FirstChild != null)
                    if (node.FirstChild.NodeType == XmlNodeType.Text || node.FirstChild.NodeType == XmlNodeType.CDATA)
                        selectExpr = node.FirstChild.Value.Trim();
            if (selectExpr == null && xmlDefinition.HasAttribute("ParentPageCode"))
                selectExpr = "//*[@Name='" + xmlDefinition.GetAttribute("ParentPageCode") + "']/Page";
        }
        private Template GetTemplate(string name, Stack<string> embedStack)
        {
            if (templates.ContainsKey(name) && !ContentCache.IsContentCacheDisabled)
                return templates[name];

            Template template;

            if (embedStack == null)
                embedStack = new Stack<string>();
            if (embedStack.Contains(name))
            {
                template = new Template("[Circular dependency detected in template heirarchy at \"" + name + "\"]");
            }
            else
            {
                embedStack.Push(name);

                XmlElement node = (XmlElement)templatesDoc.SelectSingleNode("/Templates/Template[@Name='" + name + "']");
                if (node == null)
                {
                    embedStack.Pop();
                    template = new Template("[The requested template \"" + name + "\" was not found in template definition file]");
                }
                else
                {
                    string text = "";
                    if (node.HasAttribute("Master"))
                    {
                        text = GetTemplate(node.GetAttribute("Master"), embedStack).Text;

                        XmlNodeList nodes = node.SelectNodes("Replace");
                        foreach (XmlElement repl in nodes)
                        {
                            if (!repl.HasAttribute("PlaceHolderName"))
                                continue;

                            string phtext;
                            if (repl.HasAttribute("Template"))
                                phtext = GetTemplate(repl.GetAttribute("Template"), embedStack).Text;
                            else if (repl.HasAttribute("File"))
                                phtext = WebUtility.CacheTextFile("resources/templates/" + repl.GetAttribute("File"));
                            else if (repl.FirstChild == null)
                                phtext = "";
                            else if (repl.FirstChild.NodeType == XmlNodeType.Text || repl.FirstChild.NodeType == XmlNodeType.CDATA)
                                phtext = repl.FirstChild.Value;
                            else
                                phtext = "";

                            text = Regex.Replace(text, PlaceHolder.GetRegexPattern(repl.GetAttribute("PlaceHolderName")), phtext, RegexOptions.IgnoreCase);
                        }
                    }
                    else if (node.HasAttribute("File"))
                    {
                        string path = WebUtility.MapPath("resources/templates/" + node.GetAttribute("File"));
                        if (!File.Exists(path))
                            text = "[Template file \"" + node.GetAttribute("File") + "\" does not exist]";
                        else
                            text = WebUtility.CacheTextFile("resources/templates/" + node.GetAttribute("File"));
                    }
                    else
                    {
                        if (node.FirstChild == null)
                            text = "";
                        else if (node.FirstChild.NodeType == XmlNodeType.Text || node.FirstChild.NodeType == XmlNodeType.CDATA)
                            text = node.FirstChild.Value;
                        else
                            text = "";
                    }
                    embedStack.Pop();

                    template = new Template(text);
                }
            }
            templates[name] = template;
            return template;
        }