技术文章 > C#编写GOOGLESITEMAP.XML源代码

C#编写GOOGLESITEMAP.XML源代码

2018-10-18 00:39

文档管理软件,文档管理系统,知识管理系统,档案管理系统的技术资料:

C#编写Google Sitemap.xml源代码

文件1 Sitemap.aspx
<%@ Page Language=“C#“ AutoEventWireup=“true“ CodeFile=“Sitemap.aspx.cs“ Inherits=“_Home_Sitemap“ %>

文件2 Sitemap.aspx.cs
[CODE]
csharp
using System;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;

using MYSPACE.Basic;
using MYSPACE.Entity;

public class MySitemap : SQLCommon
{
public string Execute()
{
StringBuilder sb = new StringBuilder();
string sql = “SELECT * FROM [MYDATABASE].[dbo].[tblPAGES] ORDER BY id DESC“;
SqlDataReader sr = SQLHelper.ExecuteReader(sql);
while(sr.Read())
{
DateTime dt = (DateTime)sr[“dDate“];
sb.AppendLine(“<url>“);
sb.AppendLine(“<loc>“ + sr[“cHref“].ToString() + “<loc>“);
sb.AppendLine(“<lastmod>“ + dt.ToString(“yyyy-MM-dd“) + “<lastmod>“);
sb.AppendLine(“<changefreq>daily<changefreq>“);
sb.AppendLine(“<priority>“ + String.Format(“{0:F1}“,Double.Parse(sr[“fPriority“].ToString())) + “<priority>“);
sb.AppendLine(“</url>“);
}
sr.Close();
return sb.ToString();
}
}

public partial class _Home_Sitemap : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{
MySitemap my = new MySitemap();
StringBuilder sb = new StringBuilder();

sb.AppendLine(“<?xml version=\“1.0\“ encoding=\“UTF-8\“?>“);
sb.AppendLine(“<urlset xmlns=\“http://www.google.com/schemas/sitemap/0.84\“ >“);
sb.Append(my.Execute());
sb.AppendLine(“</urlset>“);

Response.Write(sb.ToString());
}

}

[/CODE]