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;CodeAs 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:
< Head Runat = "Server"> < Title > </ Title > </ Head > < Body > < Form ID = "Form1" Runat = "Server"> < Div > < ASP : Bulletedlist ID = "Linklist" Displaymode = "HYPERLINK" Runat = "Server"> </ ASP : Bulletedlist > </ Div > </ Form > </ Body > </ Html >
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 URLsPublic voidShowurl (){XelementXelement =Xelement. Load (server. mappath ("Web. sitemap"));VaRUrllist = xelement. descendants (). attributes (). Where (x => X. Name ="Url")
. Select (x => X. value );Foreach(StringItemInUrllist ){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 use LINQ to display URL and titlePublic voidShowurlandtitle (){XelementXelement =Xelement. Load (server. mappath ("Web. sitemap"));VaRUrlandtitle = xelement. descendants (). Where (element =>
Element. lastattribute. Name. localname. Contains ("Url"). Select (nD =>New{Title = Nd. Attribute ("Title"). Value, url = Nd. Attribute ("Url"). Value });Foreach(VaRItemInUrlandtitle ){ListitemI =NewListitem(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: displays the specified dataPublic voidShowpart (){XelementXelement =Xelement. Load (server. mappath ("Web. sitemap"));VaRNode = xelement. descendants (). Where (SEL =>
(String) SEL. Attribute ("Title") ="Favorite social sites"). Selectnames (SEL => SEL. Elements (). Select (nD =>
New{Title = Nd. Attribute ("Title"). Value, url = Nd. Attribute ("Url"). Value });Foreach(VaRItemInNode ){ListitemI =NewListitem(Item. Title, item. url );This. Linklist. Items. Add (I) ;}# endregion
The running effect is as follows:
There are a lot of related operations on LINQ to XML.ArticleHere, I am also the most basic operation. If you are a beginner and think of a higher level, it is recommended that you go to msdn to learn;