Un sitemap és un llistat de URLs que té un format definit per la Sitemap.org i que s'envia als motors de cerca per tal que les indexin. El seu format és el següent:
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>http://www.example.com/</loc>
<lastmod>2005-01-01</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>http://www.example.com/</loc>
<lastmod>YYYY-MM-DD</lastmod>
<changefreq>weekly</changefreq>
</url>
</urlset>
He fet un petit programa en C# que genera aquest XML, he vist exemples on es fan servir objectes XMLDocument i es van afegint nodes fills etc... Donat que no és gaire complicat jo ho fet en cadenes de text directament. Apart d'això només s'ha de posar el Content-Type que toca: text/xml El codi a continuació
public class Sitemap : IHttpHandler
{
private clsConfig Conf = new clsConfig();
private string ConnStr;
private HttpResponse Resp;
private HttpRequest Req;
public void ProcessRequest(HttpContext context)
{
StringBuilder SB = new StringBuilder();
Conf.Initialize();
ConnStr = Conf.GetVal("CadConn");
Resp = context.Response;
Req = context.Request;
clsPosts Posts = new clsPosts(ConnStr);
DataTable TBL;
string SQL = "";
Dictionary<string, string> Params = new Dictionary<string, string>();
Resp.ContentType = "text/xml";
SB.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + Environment.NewLine);
SB.Append("<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">" + Environment.NewLine);
SB.Append("<url>" + Environment.NewLine);
SB.Append("<loc>http://blog.canalda.net/</loc>" + Environment.NewLine);
SB.AppendFormat("<lastmod>{0}</lastmod>" + Environment.NewLine, DateTime.Now.ToString("yyyy-MM-dd"));
SB.Append("<changefreq>daily</changefreq>" + Environment.NewLine);
SB.Append("</url>" + Environment.NewLine);
SQL = "SELECT IdPost, DataMod FROM Posts WHERE Actiu=1;";
TBL = Posts.Exec(SQL);
foreach (DataRow DR in TBL.Rows)
{
SB.Append("<url>" + Environment.NewLine);
SB.AppendFormat("<loc>http://blog.canalda.net/Post.aspx?IdPost={0}</loc>{1}",
DR["IdPost"].ToString() , Environment.NewLine);
SB.AppendFormat("<lastmod>{0}</lastmod>{1}",
DR.Field<DateTime>("DataMod").ToString("yyyy-MM-dd"),
Environment.NewLine);
SB.Append("<changefreq>monthly</changefreq>" + Environment.NewLine);
SB.Append("</url>" + Environment.NewLine);
}
SB.Append("</urlset>" + Environment.NewLine);
Resp.Write(SB.ToString());
}
public bool IsReusable
{
get
{
return false;
}
}
}
|