There is nothing today. I can see on the Internet that using LINQ to XML to operate sitemap. I feel very useful. I will write it out and share it with you. Although it is very simple, I still want to write it, it may be helpful for people to do projects in the future. If you are a normal person, you can leave it alone. If you are a beginner, you can check it out;
1. First, create a web. sitemap XML file. The Code is as follows:
<?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>
2. in step 2, we add a control to the page file. Here we use bulletedlist and set its display mode to hyperlink. The Code is as follows:
3. Use LINQ to XML to read all the URLs in the XML file. The Code is as follows:
# Region use LINQ to display all URLs public void showurl () {xelement = xelement. load (server. mappath ("Web. sitemap "); var urllist = xelement. descendants (). attributes (). where (x => X. name = "url ")
.Select(x => x.Value); foreach (string item in urllist) { this.linkList.Items.Add(item); } } #endregion
The running result is as follows:
4. Use LINQ to XML to read the URL and title and display them. The Code is as follows:
# Region uses LINQ to display the URL and title Public void showurlandtitle () {xelement = xelement. load (server. mappath ("Web. sitemap "); var urlandtitle = xelement. descendants (). where (element =>
element.LastAttribute.Name.LocalName.Contains("url")) .Select(nd => new { title = nd.Attribute("title").Value, url = nd.Attribute("url").Value }); foreach (var item in urlandTitle) { ListItem i = new ListItem(item.title, item.url); this.linkList.Items.Add(i); } } #endregion
The running effect is as follows:
5. Read the specified data using LINQ to XML. The Code is as follows:
# Region display the specified data information public void showpart () {xelement = xelement. load (server. mappath ("Web. sitemap "); var node = xelement. descendants (). 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 item in node) { ListItem i = new ListItem(item.title, item.url); this.linkList.Items.Add(i); } } #endregion
The running effect is as follows:
There are many related articles on the operations of LINQ to XML. I am also the most basic operation here. If you are a beginner and think of a higher level, it is recommended that you go to msdn to learn;