[Skip to Content]

Published Wednesday 18 February 2009
Categories: Projects |

Share this Share on Facebook Share on Twitter Share on Google Plus
Post View Counter 290

Below is the code to make an .aspx page written in C# which scans all folders on a website and generates a xml site map which google can read for all .htm or .html pages. If you want to add other file types, edit the in dirInfo.GetFiles("*.htm")) section to the file extention required.

[more]

<%@ Page Language="C#" ContentType="text/xml" ResponseEncoding="utf-8"  %>
<%@ Import Namespace="System.IO" %>
<script language="C#" runat="server">
public string makeurl(string sInput) {
 return Regex.Replace(sInput.ToLower(),"[^a-z0-9]", "_");
}
string baseurl = "";
string foldername = "";
private void Page_Load(object sender, System.EventArgs e)
 {
 StringBuilder sb = new StringBuilder();
 baseurl = "http://" + Request.ServerVariables["HTTP_HOST"];
 foldername = Server.MapPath("\\");
 sb.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>" + Environment.NewLine);
 sb.Append("<urlset xmlns=\"http://www.google.com/schemas/sitemap/0.84\">" + Environment.NewLine);
    processFolderXML(foldername, sb);
 sb.Append("</urlset>");
 Response.Write(sb.ToString());
}
private void processFolderXML(string folder, StringBuilder root)
  {
      DirectoryInfo dirInfo = new DirectoryInfo(folder);
      foreach(DirectoryInfo subDir in dirInfo.GetDirectories())
      {
          processFolderXML(subDir.FullName, root);
      }
     
      foreach(FileInfo containedFile in dirInfo.GetFiles("*.htm"))
      {
          root.Append("<url>" + Environment.NewLine);
    if (dirInfo.FullName.Replace(foldername,"").Length > 0) {
     root.Append("   <loc>" + baseurl + "/" + dirInfo.FullName.Replace(foldername,"").Replace("\\","/") + "/" +  containedFile.Name + "</loc>" + Environment.NewLine);
    } else {
     root.Append("   <loc>" + baseurl + "/" + containedFile.Name + "</loc>" + Environment.NewLine);
    }
    root.Append("   <lastmod>" + containedFile.LastWriteTime.ToString("yyyy-MM-dd") + "</lastmod>" + Environment.NewLine);
    root.Append("   <changefreq>weekly</changefreq>" + Environment.NewLine);
    root.Append("   <priority>0.8</priority>" + Environment.NewLine);
    root.Append("</url>" + Environment.NewLine);
      }
  }
</script>

 

Permalink


0 Comments



Post your comment