Querying Web.SiteMap using LINQ
The sample web.sitemap used in this article is shown below:
xml version="1.0" encoding="utf-8"?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0">
<siteMapNode title="My Favorites">
<siteMapNode title="Favorite Sites">
<siteMapNode title="ASP.NET Home" url="http://www.asp.net" />
<siteMapNode title="ASP.NET Articles" url="http://www.dotnetcurry.com"/>
<siteMapNode title="Windows Client" url="http://www.windowsclient.net" />
<siteMapNode title="Silverlight" url="http://silverlight.net" />
siteMapNode>
<siteMapNode title="Favorite Blogs">
<siteMapNode title="ScottGu Blog" url="http://weblogs.asp.net/scottgu"/>
<siteMapNode title="Technology Blog" url="http://www.devcurry.com" />
<siteMapNode title="SQL Blog" url="http://www.sqlservercurry.com" />
<siteMapNode title="Food Lovers" url="http://foodatarian.com" />
siteMapNode>
<siteMapNode title="Favorite Social Sites">
<siteMapNode title="Twitter" url="http://twitter.com/"/>
<siteMapNode title="FaceBook" url="http://www.facebook.com" />
<siteMapNode title="LinkedIn" url="http://www.linkedin.com" />
<siteMapNode title="Orkut" url="http://www.orkut.com" />
siteMapNode>
siteMapNode>
siteMap>
The Data Source for above web.sitemap is Bulleted List.
<asp:BulletedList ID="linkList" DisplayMode="HyperLink" runat="server">
asp:BulletedList>
List all Url’s in the SiteMap using LINQ
XElement xelement = XElement.Load(Server.MapPath("~/web.sitemap"));
var urlList = xelement.Descendants().Attributes()
.Where(x => x.Name == "url")
.Select(x => x.Value);
foreach (string s in urlList)
{
linkList.Items.Add(s);
}
List Url’s and Title in the SiteMap using LINQ
XElement xelement1 = XElement.Load(Server.MapPath("~/web.sitemap"));
var urlDescList = xelement1.Descendants()
.Where(element => element.LastAttribute.Name.LocalName.Contains("url"))
.Select(nd => new
{
title = nd.Attribute("title")
.Value,
url = nd.Attribute("url")
.Value
});
foreach (var v in urlDescList)
{
ListItem li = new ListItem(v.title, v.url);
linkList.Items.Add(li);
}
List Url’s and Title of a particular node of the SiteMap using LINQ
XElement xelement2 = XElement.Load(Server.MapPath("~/web.sitemap"));
var urlDescList1 = xelement2.Descendants()
// Select node with 'Favorite Social Sites'
.Where(sel => (string)sel.Attribute("title") == "Favorite Social Sites")
.SelectMany(sel => sel.Elements())
.Select(nd => new
{
title = nd.Attribute("title").Value,
url = nd.Attribute("url").Value
});
foreach (var s in urlDescList1)
{
ListItem li = new ListItem(s.title, s.url);
linkList.Items.Add(li);
}
Count the number of nodes in each parent element of SiteMap using LINQ
XElement xelement3 = XElement.Load(Server.MapPath("~/web.sitemap"));
var t = xelement3.Descendants()
.Where(x => x.LastAttribute.Name.LocalName != "url")
.Select(ele => new
{
Name = (string)ele.Attribute("title"),
Count = ele.Elements().Count()
});
Source: http://www.dotnetcurry.com/ShowArticle.aspx?ID=284&AspxAutoDetectCookieSupport=1
Thanks,
Nitin Sharma