制作sitemap的一段c#代码

本文介绍了一个用于生成XML格式站点地图的实用工具。该工具通过ASP.NET页面加载事件自动生成符合Google规范的站点地图文件,包括多个网站链接及其更新日期和优先级。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

///
/// Using this object.
///


  private void Page_Load(object sender, System.EventArgs e)
  {
   try
   {    
    // set the content type
 
    Page.Response.ContentType = "text/xml";
 
    // create a SiteMap
 
    SiteMapFeedGenerator gen = new SiteMapFeedGenerator(Page.Response.Output);
 
    gen.WriteStartDocument();
 
    
    gen.WriteItem("http://www.pointnetsolutions.com/en/blogs/Page.aspx",DateTime.Now,"1.0");
    gen.WriteItem("http://www.pointnetsolutions.com/en/help/Page.aspx",DateTime.Now,"1.0");
    gen.WriteItem("http://www.pointnetsolutions.com/default.aspx",DateTime.Now,"0.6");
    gen.WriteItem("http://www.pointnetsolutions.com/en/Groups/page.aspx",DateTime.Now,"1.0");
    gen.WriteItem("http://www.pointnetsolutions.com/en/about/page.aspx",DateTime.Now,"0.8");
    gen.WriteItem("http://www.pointnetsolutions.com/options.aspx?lang=En",DateTime.Now,"0.8");
    
    gen.WriteEndDocument();
    gen.Close();
   }
   catch(Exception ex)
   {
    
   }
  }


///
/// Some parts of this code are form unknown sources,
/// This code is not 100% from me. I modified it from older
/// projects of mine. Please keep this in mind when using this
/// code.
///
/// Alexandre Brisebois
///

        public class SiteMapFeedGenerator
  {
   XmlTextWriter writer;
   public SiteMapFeedGenerator( System.IO.Stream stream, System.Text.Encoding encoding )
   {
    writer = new XmlTextWriter(stream, encoding);
    writer.Formatting = Formatting.Indented;
   }
   public SiteMapFeedGenerator( System.IO.TextWriter w )
   {
    writer = new XmlTextWriter(w);
    writer.Formatting = Formatting.Indented;
   }
   /// <summary>
   /// Writes the beginning of the SiteMap document
   /// </summary>
   public void WriteStartDocument()
   {
    writer.WriteStartDocument();
    writer.WriteStartElement("urlset");
    writer.WriteAttributeString("xmlns","http://www.google.com/schemas/sitemap/0.84");
   }
   /// <summary>
   /// Writes the end of the SiteMap document
   /// </summary>
   public void WriteEndDocument()
   {
    writer.WriteEndElement();
    writer.WriteEndDocument();
   }
   /// <summary>
   /// Closes this stream and the underlying stream
   /// </summary>
   public void Close()
   {
    writer.Flush();
    writer.Close();
   }
      
   public void WriteItem(string link, DateTime publishedDate,string priority)
   {
    writer.WriteStartElement("url");
    writer.WriteElementString("loc",link);
    writer.WriteElementString("lastmod",formatDate(publishedDate));
    writer.WriteElementString("changefreq","always");
    if(priority == null)
    {
     writer.WriteElementString("priority","0.8");
    }
    else
    {
     writer.WriteElementString("priority",priority);
    }
    writer.WriteEndElement();
   }

   public string formatDate(DateTime d)
   {
    return d.ToString("s")+ "+00:00";
   }
  }
 
     //Calculate Percentage rating Method
     private string percent(int current, int total)
        {
            if(current > total-5)
            {
             return "1.0";
            }
            else if(current > total - 20)
            {
             return "0.9";
            }
            else if (current > total - 50)
            {
             return "0.8";
            }
            else if (current > total - 100)
            {
             return "0.7";
            }
            else if (current > total - 200)
            {
             return "0.6";
            }
            else if (current > total - 300)
            {
             return "0.5";
            }
            else if (current > total - 400)
            {
             return "0.4";
            }
            else if (current > total - 500)
            {
             return "0.3";
            }
            else if (current > total - 1500)
            {
             return "0.2";
            }
            else
            {
             return "0.1";
            }    
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值