Linq to XML---网站地图和RSS Feed

网站地图的作用是让搜索引擎尽快的,更多的收录网站的各个网页。


这里我们首先要明白一个基本的原理,搜索引擎的爬行方式。整个互联网就像一张纵横交错的“网”:网的各个节点就是各个网页,而各个网页之间通过url相互连接。蜘蛛可以从一个网页出发,通过该网页上的url,爬到另一个网页;再通过另一个网页上的url,再爬到更多的网页……,以此类推。但如果是一个新发布的网站,可能就没有其他url指向它,那么它就永远不会被“爬到”(收录)。为了解决这个问题,新站可以自己主动向搜索引擎提交url,申请蜘蛛前来抓取(Google申请网址:),但申请时一般只会提交一个主页的url。


为了让所有的url(尤其是动态生成的)都能被蜘蛛快捷便利的检索到,我们就需要提供一个全面完整、架构清晰和更新及时的网站地图。(网站地图的更多信息)。

 

和处理重复内容的robots.txt文件,我们通过.ashx文件来生成一个基于sitemaps.org的xml格式的网站地图。网站地图生成之后,我们就可以向Google等搜索引擎提交。大量的文章证实,提交网站地图将极大的提高网站的收录速度和深度。其他几乎所有的SEO方法,都有可能效果难以证实、失效甚至带来副作用,但提交网站地图除外!

 

Linq to XML为我们带来了近乎完美的操作体验。

隐藏行号 复制代码 站点地图 XML
  1. using System;
    
  2. using System.Web;
    
  3. using System.Xml;
    
  4. using System.Xml.Linq;
    
  5. namespace WebApplication1
    
  6. {
    
  7.     public class RssHttpHandler : IHttpHandler
    
  8.     {
    
  9.         public void ProcessRequest(HttpContext context)
    
  10.         {
    
  11.             context.Response.ContentType = "text/xml";
    
  12.             context.Response.Write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
    
  13.             XElement rssFeed = new XElement("rss", new XAttribute("version", "2.0"));
    
  14.             string fixedUrl = "http://www.freeflying.com/article";
    
  15.             string wholeUrl = string.Empty;
    
  16.             XElement channel = new XElement("channel",
    
  17.                 new XElement("title", "freeflying"),
    
  18.                 new XElement("link", fixedUrl),
    
  19.                 new XElement("description", "the website for dream flying freely"),
    
  20.                 new XElement("pubDate", DateTime.Now.ToString())
    
  21.                 );
    
  22.             foreach (var article in Articles.GetArticles())
    
  23.             {
    
  24.                 XElement item = new XElement("item");
    
  25.                 XElement title = new XElement("title", article.Title);
    
  26.                 wholeUrl = string.Format("{0}?id={1}&catelog={2}", fixedUrl, article.ID, article.Catelog);
    
  27.                 XElement link = new XElement("link", wholeUrl);
    
  28.                 XElement description = new XElement("description", article.Description);
    
  29.                 XElement pubDate = new XElement("pubDate", article.LastMod.ToString());
    
  30.                 item.Add(title, link, description, pubDate);
    
  31.                 channel.Add(item);
    
  32.             }
    
  33.             rssFeed.Add(channel);
    
  34.             context.Response.Write(rssFeed);
    
  35.         }
    
  36.         public bool IsReusable
    
  37.         {
    
  38.             get { return false; }
    
  39.         }
    
  40.   
    
  41.     }
    
  42. }
    

 

同样还将使用到xml技术的还有RSS

 

Rss
  1. using System;
    
  2. using System.Web;
    
  3. using System.Xml;
    
  4. using System.Xml.Linq;
    
  5. namespace WebApplication1
    
  6. {
    
  7.     public class RssHttpHandler : IHttpHandler
    
  8.     {
    
  9.         public void ProcessRequest(HttpContext context)
    
  10.         {
    
  11.             context.Response.ContentType = "text/xml";
    
  12.             context.Response.Write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
    
  13.             XElement rssFeed = new XElement("rss", new XAttribute("version", "2.0"));
    
  14.             string fixedUrl = "http://www.freeflying.com/article";
    
  15.             string wholeUrl = string.Empty;
    
  16.             XElement channel = new XElement("channel",
    
  17.                 new XElement("title", "freeflying"),
    
  18.                 new XElement("link", fixedUrl),
    
  19.                 new XElement("description", "the website for dream flying freely"),
    
  20.                 new XElement("pubDate", DateTime.Now.ToString())
    
  21.                 );
    
  22.             foreach (var article in Articles.GetArticles())
    
  23.             {
    
  24.                 XElement item = new XElement("item");
    
  25.                 XElement title = new XElement("title", article.Title);
    
  26.                 wholeUrl = string.Format("{0}?id={1}&catelog={2}", fixedUrl, article.ID, article.Catelog);
    
  27.                 XElement link = new XElement("link", wholeUrl);
    
  28.                 XElement description = new XElement("description", article.Description);
    
  29.                 XElement pubDate = new XElement("pubDate", article.LastMod.ToString());
    
  30.                 item.Add(title, link, description, pubDate);
    
  31.                 channel.Add(item);
    
  32.             }
    
  33.             rssFeed.Add(channel);
    
  34.             context.Response.Write(rssFeed);
    
  35.         }
    
  36.         public bool IsReusable
    
  37.         {
    
  38.             get { return false; }
    
  39.         }
    
  40.     }
    
  41. }
    

 

本文转自:http://www.cnblogs.com/freeflying/archive/2010/02/26/1674557.html

 

下载本文示例代码

posted @ 2010-05-05 14:42  luckdv  阅读(480)  评论(0)    收藏  举报