protected virtual SiteMapNode CreateNodeFromElement(XElement element, int currentLevel)
        {
            var attributes = new NameValueCollection();

            foreach (var a in element.Attributes())
            {
                attributes.Add(a.Name.ToString(), a.Value);
            }

            string uri;

            try
            {
                if (element.Attribute("uri") != null)
                {
                    uri = element.Attribute("uri").Value;
                }
                else if (element.Attribute("pageId") != null)
                {
                    uri = element.Attribute("pageId").Value;
                }
                else
                {
                    uri = "";
                }
            }
            catch
            {
                _logger.Debug("exception while retrieving uri", LoggingCategory.General);
                uri = "";
            }
            SiteMapNode childNode = new TridionSiteMapNode(this,
                                                           element.Attribute("id").Value,          //key
                                                           uri,
                                                           element.Attribute("url").Value,         //url
                                                           element.Attribute("title").Value,       //title
                                                           element.Attribute("description").Value, //description
                                                           null,                                   //roles
                                                           attributes,                             //attributes
                                                           null,                                   //explicitresourceKeys
                                                           null)
            {
                Level = currentLevel
            };                                  // implicitresourceKey

            NodeDictionary.Add(childNode.Key, childNode);

            return(childNode);
        }
        private static void AddMenuItem(StringBuilder sb, TridionSiteMapNode node, string submenu)
        {
            TagBuilder li = new TagBuilder("li");
            TagBuilder a = new TagBuilder("a");
            a.Attributes.Add("href", node.ResolvedUrl);
            a.InnerHtml = node.Title;

            if (submenu == null)
            {
                li.InnerHtml = a.ToString();
            }
            else
            {
                li.InnerHtml = a.ToString() + submenu;
            }
            sb.Append(li.ToString());
        }
        public static string GetTitle(TridionSiteMapNode node, DD4T.ContentModel.Factories.IPageFactory pageFactory, IComponentFactory cmpFactory)
        {
            string title = null;

            string pageUri = null;
            if (node.Attributes["type"].Equals("64"))
            {
                pageUri = node.Attributes["id"];
            }
            else
            {
                var landingPageNode = node.ChildNodes
                    .Cast<TridionSiteMapNode>()
                    .FirstOrDefault(tn => tn.Attributes["type"].Equals("64") && tn.Title.StartsWith("000 "));

                if (landingPageNode != null)
                {
                    pageUri = landingPageNode.Attributes["id"];
                }
            }

            if (!String.IsNullOrEmpty(pageUri))
            {
                IPage landingPage;
                if (pageFactory.TryGetPage(pageUri, out landingPage))
                {
                    var landingCp = landingPage.ComponentPresentations.FirstOrDefault();
                    if (landingCp != null)
                    {
                        var component = cmpFactory.GetComponent(landingCp.Component.Id, landingCp.ComponentTemplate.Id);
                        title = component.Fields["Heading"].Value;
                    }
                }
            }

            if (String.IsNullOrEmpty(title))
            {
                title = node.Title.Remove(0, 4);
            }
            return title;
        }
        private SiteMapNode ReadSitemapFromXml(string sitemapUrl)
        {
            _logger.Debug(">>ReadSitemapFromXml", LoggingCategory.Performance);
            SiteMapNode rootNode = null;

            NodeDictionary = new Dictionary <string, SiteMapNode>();

            string sitemap;

            if (!_pageFactory.TryFindPageContent(sitemapUrl, out sitemap))
            {
                sitemap = emptySiteMapString();
            }
            _logger.Debug(string.Format("loaded sitemap with url {0}, length {1}", sitemapUrl, sitemap.Length), LoggingCategory.Performance);

            XDocument xDoc = XDocument.Parse(sitemap);

            _logger.Debug("parsed sitemap into XDocument", LoggingCategory.Performance);

            //XElement siteMapRoot = xDoc.Element("siteMap");
            XElement siteMapRoot = xDoc.Root;


            try
            {
                rootNode = new TridionSiteMapNode(this, String.Empty, "root_" + _pageFactory.PageProvider.PublicationId, String.Empty, String.Empty, String.Empty, new ArrayList(), new NameValueCollection(), new NameValueCollection(), String.Empty);
                _logger.Debug("created root node", LoggingCategory.Performance);
                AddNode(rootNode);
                _logger.Debug("added root node", LoggingCategory.Performance);

                //Fill down the hierarchy.
                AddChildren(rootNode, siteMapRoot.Elements(), 1);
            }
            catch (Exception e)
            {
                Exception e2 = e;
            }
            _logger.Debug("<<ReadSitemapFromXml", LoggingCategory.Performance);
            return(rootNode);
        }
        private static void AddChildMenuItems(StringBuilder sb, TridionSiteMapNode node, int deep)
        {
            // Iterate through childnodes
            if (!node.HasChildNodes)
                return;

            foreach (TridionSiteMapNode childNode in node.ChildNodes)
            {
                StringBuilder liNodes = new StringBuilder();
                if (deep > 0 && childNode.HasChildNodes)
                {
                    TagBuilder ul = new TagBuilder("ul");
                    AddChildMenuItems(liNodes, childNode, deep - 1);
                    ul.InnerHtml = liNodes.ToString();
                    AddMenuItem(sb, childNode, ul.ToString());
                }
                else
                {
                    AddMenuItem(sb, childNode, null);
                }
            }
        }
        private static TridionSiteMapNode FindMainParentNode(TridionSiteMapNode childnode)
        {
            if (childnode == null)
            {
                return null;
            }

            TridionSiteMapNode currentNode = childnode;
            while (currentNode.ParentNode != null && currentNode.Level > 2)
            {
                currentNode = (TridionSiteMapNode)currentNode.ParentNode;
            }

            return currentNode;
        }
        private static void AddMenuItem(StringBuilder sb, TridionSiteMapNode node, string submenu, string cssClass)
        {
            var li = new TagBuilder("li");
            var a = new TagBuilder("a");

            var span = new TagBuilder("span");
            span.Attributes.Add("class", cssClass);

            var span2 = new TagBuilder("span");
            span2.Attributes.Add("class", "arrow icon");

            span.InnerHtml = node.Title;

            if (cssClass != null)
                a.InnerHtml = span + span2.ToString();

            string href = null;
            if (node.Attributes["RedirectUrl"] != null)
            {
                href = node.Attributes["RedirectUrl"];
            }
            else
            {
                href = RootType.ToLower().Equals("structure") ? General.AdjustUrlToContext(node.Url) : General.AdjustUrlToContext(node.ResolvedUrl);
            }

            a.Attributes.Add("href", href);

            if (cssClass == null)
                a.InnerHtml = node.Title;

            if (submenu == null)
            {
                li.InnerHtml = a.ToString();
            }
            else
            {
                li.InnerHtml = a + submenu;
            }

            sb.AppendLine(li.ToString());
        }
 private static void AddMenuItem(StringBuilder sb, TridionSiteMapNode node, string submenu)
 {
     AddMenuItem(sb, node, submenu, null);
 }
        private static void AddChildMenuItems(StringBuilder sb, TridionSiteMapNode node, int deep, List<int> itemTypes)
        {
            // Iterate through childnodes
            if (!node.HasChildNodes)
                return;

            foreach (TridionSiteMapNode childNode in node.ChildNodes)
            {
                if (itemTypes.Contains(new TcmUri(childNode.Key).ItemTypeId))
                {
                    if (childNode.Url.Contains(ConfigurationManager.AppSettings["DD4T.DefaultPage"]))
                        continue;

                    AddMenuItem(sb, childNode, null, null);
                    if (deep > 0 && childNode.HasChildNodes)
                    {
                        var liNodes = new StringBuilder();
                        AddChildMenuItems(liNodes, childNode, deep - 1, itemTypes);
                        if (liNodes.Length > 0)
                        {
                            var ul = new TagBuilder("ul");
                            ul.InnerHtml = liNodes.ToString();
                            sb.Append(ul.ToString());
                        }
                    }
                }
            }
        }
        private SiteMapNode ReadSitemapFromXml(string sitemapUrl)
        {
            LoggerService.Debug(">>ReadSitemapFromXml", LoggingCategory.Performance);
            SiteMapNode rootNode = null;
            NodeDictionary = new Dictionary<string, SiteMapNode>();

            string sitemap;
            if (!PageFactory.TryFindPageContent(sitemapUrl, out sitemap))
            {
                sitemap = emptySiteMapString();
            }
            LoggerService.Debug(string.Format("loaded sitemap with url {0}, length {1}", sitemapUrl, sitemap.Length), LoggingCategory.Performance);
 
            XDocument xDoc = XDocument.Parse(sitemap);

            LoggerService.Debug("parsed sitemap into XDocument", LoggingCategory.Performance);

            //XElement siteMapRoot = xDoc.Element("siteMap");
            XElement siteMapRoot = xDoc.Root;


            try
            {
                rootNode = new TridionSiteMapNode(this, String.Empty, "root_" + PageFactory.PageProvider.PublicationId, String.Empty, String.Empty, String.Empty, new ArrayList(), new NameValueCollection(), new NameValueCollection(), String.Empty);
                LoggerService.Debug("created root node",LoggingCategory.Performance);
                AddNode(rootNode);
                LoggerService.Debug("added root node", LoggingCategory.Performance);

                //Fill down the hierarchy.
                AddChildren(rootNode, siteMapRoot.Elements(), 1);
            }
            catch (Exception e)
            {
                Exception e2 = e;
            }
            LoggerService.Debug("<<ReadSitemapFromXml", LoggingCategory.Performance);
            return rootNode;
        }
        protected virtual SiteMapNode CreateNodeFromElement(XElement element, int currentLevel)
        {
            var attributes = new NameValueCollection();
            foreach (var a in element.Attributes())
            {
                attributes.Add(a.Name.ToString(), a.Value);
            }

            string uri;
            try
            {
                if (element.Attribute("uri") != null)
                    uri = element.Attribute("uri").Value;
                else if (element.Attribute("pageId") != null)
                    uri = element.Attribute("pageId").Value;
                else
                    uri = "";
            }
            catch
            {
                LoggerService.Debug("exception while retrieving uri", LoggingCategory.General);
                uri = "";
            }
            SiteMapNode childNode = new TridionSiteMapNode(this,
                element.Attribute("id").Value, //key
                uri,
                element.Attribute("url").Value, //url
                element.Attribute("title").Value, //title
                element.Attribute("description").Value, //description
                null, //roles
                attributes, //attributes
                null, //explicitresourceKeys
                null) { Level = currentLevel }; // implicitresourceKey

            NodeDictionary.Add(childNode.Key, childNode);

            return childNode;
        }