.net c# Code for generating a SiteMap
Enjoy
///Using
Page.Response.ContentType = "text/xml";
SiteMapFeedGenerator gen = new
SiteMapFeedGenerator(Page.Response.Output);
gen.WriteStartDocument();
//Add items to the list
gen.WriteItem("http://www.pointnetsolutions.com/default.aspx",DateTime.Now);
gen.WriteEndDocument();
gen.Close();
///Object
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)
{
writer.WriteStartElement("url");
writer.WriteElementString("loc",link);
writer.WriteElementString("lastmod",formatDate(publishedDate));
writer.WriteElementString("changefreq","always");
writer.WriteElementString("priority","0.8");
writer.WriteEndElement();
}
public string formatDate(DateTime d)
{
string date = "";
date = d.Year+ "-";
if(d.Month.ToString().Length == 1)
{
date += "0"+d.Month +"-";
}
else
{
date += d.Month +"-";
}
if(d.Day.ToString().Length == 1)
{
date += "0"+d.Month;
}
else
{
date += d.Day;
}
return date;
}
}