• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 217
  • Last Modified:

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

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?

Thanks
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.WriteStartDocument();
                writer.WriteStartElement("urlset");
                writer.WriteAttributeString("xmlns", "http://www.google.com/schemas/sitemap/0.84");
                SiteMapNode root = SiteMap.RootNode;
                WriteNode(root, writer, 0);
                writer.WriteEndElement(); // urlset
                writer.WriteEndDocument();
            }
        }

        public bool IsReusable
        {
            get
            {
                return true;
            }
        }

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

            writer.WriteStartElement("url");

            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))
            {
                try
                {
                    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

0
DotNetChano
Asked:
DotNetChano
1 Solution
 
DotNetChanoAuthor Commented:
No body knows how to fix this?
0
 
Kamal KhaleefaInformation Security SpecialistCommented:
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
ex:
priority=0.9
changeFreq=daily
0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now