Need to make this C# file include "details" when generating Google Sitemap file with .ashx file

Posted on 2011-04-19
Last Modified: 2012-05-11
The attached C# file currently builds a Google friendly sitemap file out of a web.sitemap file. Unfortunately it is only grabbing the url and Title, it doesnt include the description or the modified date or crawl rate.

Does anyone know how to have this file grab that data too?

public class gsitemap1 : IHttpHandler
        private const int MaxDepth = 6;

        public void ProcessRequest(HttpContext context)
            HttpResponse response = context.Response;
            response.ContentType = "text/xml";

            using (XmlTextWriter writer = new XmlTextWriter(response.OutputStream, Encoding.UTF8))
                writer.Formatting = Formatting.Indented;
                writer.WriteAttributeString("xmlns", "");
                SiteMapNode root = SiteMap.RootNode;
                WriteNode(root, writer, 0);
                writer.WriteEndElement(); // urlset

        public bool IsReusable
                return true;

        private void WriteNode(SiteMapNode node, XmlTextWriter writer, int depth)
            if (depth > MaxDepth) return;


            NameValueCollection vars = HttpContext.Current.Request.ServerVariables;
            string port = vars["SERVER_PORT"];
            port = (port == "80") ? String.Empty : ':' + port;
            string protocol = (vars["SERVER_PORT_SECURE"] == "1") ? "https://" : "http://";
            writer.WriteElementString("loc", protocol + vars["SERVER_NAME"] + port + node.Url);

            string lastMod = node["lastModified"];
            if (String.IsNullOrEmpty(lastMod))
                    string physicalPath = HttpContext.Current.Request.MapPath(node.Url);
                    if (File.Exists(physicalPath))
                        DateTime lastModified = File.GetLastWriteTimeUtc(physicalPath);
                        lastMod = lastModified.ToString("yyyy-MM-ddThh:mm:ss.fffZ");
                catch { }
            if (!String.IsNullOrEmpty(lastMod))
                writer.WriteElementString("lastmod", lastMod);

            string changeFreq = node["changeFrequency"];
            if (!String.IsNullOrEmpty(changeFreq))
                writer.WriteElementString("changefreq", changeFreq);

            string priority = node["priority"];
            if (!String.IsNullOrEmpty(priority))
                writer.WriteElementString("priority", priority);

            writer.WriteEndElement(); // url

            int subNodeDepth = depth + 1;
            foreach (SiteMapNode subNode in node.ChildNodes)
                WriteNode(subNode, writer, subNodeDepth);

Open in new window

Question by:DotNetChano
    LVL 4

    Author Comment

    No body knows how to fix this?
    LVL 16

    Accepted Solution

    try to remove the if statement for them and always include it

      writer.WriteElementString("changefreq", changeFreq);

        writer.WriteElementString("priority", priority);
    and you set the changeFreq and priority manually

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now