zoukankan      html  css  js  c++  java
  • 牛人莫入使用LINQ查询ASP.NET中的Sitemap

    今天没有什么事,在网上看到用LINQ to XML来操作Sitemap,自己感觉很有用,所有就写出来与大家一起分享,虽然很简单,但是我还是要写,可能对以后大家做项目有一点帮助;如是你是牛X的人,你可以不看,如果你是初学者,推荐你可以看看;

    1.首先我们要创建一个Web.Sitemap XML文件;代码如下所示:

    <?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.第二步,我们为页面文件中添加一个控件,在这里我用的是bulletedList,并把它的显示方式设为Hyperlink,代码如下:

    <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.用LINQ to XML来读出XML文件中的所有的URL,代码如下所示:
    #region 用LINQ显示出所有的URL
         public void ShowURL()
         {
             XElement 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
    运行效果出下:
    image 
    4.用Linq to Xml 来读取Url和title 并显示,代码如下所示
    #region 用LINQ显示URL和Title
           public void ShowUrlandTitle()
           {
               XElement 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
    运行效果如下所示:
    image 
    5.用Linq to xml读出指定的数据,代码如下所示:
    #region 显示指定的数据信息
    
            public void ShowPart()
            {
                XElement 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

    运行效果如下:

    image

    关于linq to xml的操作有很多相关的文章,我在这里也是最基本的操作,如果你是初学者想到更高的层次,建议可以到MSDN上去学习;

    
    
  • 相关阅读:
    javaScript的一些兼容性问题
    JavaScript的基本规范
    数组去重的方法有哪些?
    封装on emit off方法(observer)
    对数据进行单元格合并处理的函数
    判断浏览器的内核的函数方法
    bootstrap与vue的区别是什么?(十七)
    Redis介绍及使用(八)
    mysql常用sql语法
    idea使用maven下载jar包,出现证书校验问题问题,unable to find valid certification path to requested target
  • 原文地址:https://www.cnblogs.com/caodaiming/p/1434087.html
Copyright © 2011-2022 走看看