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

            writer.WriteStartElement("url");
            string appUrl = SearchSiteMapProviderBase.GenerateUrl(node.Url);

            writer.WriteElementString("loc", appUrl);

            string lastMod = node["lastModified"];

            if (String.IsNullOrEmpty(lastMod) && (HttpContext.Current != null))
            {
                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", CultureInfo.InvariantCulture);
                }
            }
            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);
            }
        }
Example #2
0
        public override void WriteNodes(SearchSiteMapHandler handler, XmlTextWriter writer)
        {
            IEnumerable results = DataQuery();

            if (results != null)
            {
                string appUrl = SearchSiteMapProviderBase.GenerateUrl(HttpContext.Current.Request.ApplicationPath + "/");

                //Format is : http://server/site/page.aspx/value1/value2 etc
                if (_pathInfoFormat == true)
                {
                    _targetUrlseparator            = "/";
                    _queryStringDataFieldSeparator = "/";
                }

                foreach (object o in results)
                {
                    String targetUrlSuffix = "";

                    //Do this once once
                    //Generate query string data fields
                    if ((_queryStringDataFields == null) || (_queryStringDataFields.Count == 0))
                    {
                        FieldInfo[] fis = o.GetType().GetFields();
                        foreach (FieldInfo fi in fis)
                        {
                            //Skip sitemap specific fields
                            if (String.Compare(fi.Name, _lastModifiedDataField, StringComparison.Ordinal) == 0)
                            {
                                continue;
                            }

                            if (String.Compare(fi.Name, _changeFrequencyDataField, StringComparison.Ordinal) == 0)
                            {
                                continue;
                            }

                            if (String.Compare(fi.Name, _priorityDataField, StringComparison.Ordinal) == 0)
                            {
                                continue;
                            }

                            _queryStringDataFields.Add(fi.Name);
                        }
                    }

                    if ((_queryStringDataFields != null) && (_queryStringDataFields.Count > 0))
                    {
                        // Is format string present?
                        if (!String.IsNullOrEmpty(_queryStringDataFormatString))
                        {
                            List <object> _queryStringValues = new List <object>();

                            foreach (String queryStringName in _queryStringDataFields)
                            {
                                _queryStringValues.Add(DataEval(o, queryStringName).ToString());
                            }

                            targetUrlSuffix = String.Format(CultureInfo.InvariantCulture, _queryStringDataFormatString, _queryStringValues.ToArray());
                        }
                        else
                        {
                            foreach (String queryStringName in _queryStringDataFields)
                            {
                                String queryStringValue     = DataEval(o, queryStringName).ToString();
                                String queryStringNameValue = "";

                                if (!_pathInfoFormat)
                                {
                                    queryStringNameValue = queryStringName + "=" + queryStringValue;
                                }
                                else
                                {
                                    queryStringNameValue = queryStringValue;
                                }

                                if (targetUrlSuffix.Length > 0)
                                {
                                    targetUrlSuffix += _queryStringDataFieldSeparator;
                                }

                                targetUrlSuffix += queryStringNameValue;
                            }
                        }
                    }

                    String url = _targetUrl + _targetUrlseparator + targetUrlSuffix;

                    writer.WriteStartElement("url");
                    writer.WriteElementString("loc", appUrl + url);

                    if (!String.IsNullOrEmpty(_lastModifiedDataField))
                    {
                        string lastmodifiedValue = DataEval(o, _lastModifiedDataField).ToString();
                        if (!String.IsNullOrEmpty(lastmodifiedValue))
                        {
                            writer.WriteElementString("lastmod", lastmodifiedValue);
                        }
                    }

                    if (!String.IsNullOrEmpty(_changeFrequencyDataField))
                    {
                        string changeFrequencyValue = DataEval(o, _changeFrequencyDataField).ToString();
                        if (!String.IsNullOrEmpty(changeFrequencyValue))
                        {
                            writer.WriteElementString("changefreq", changeFrequencyValue);
                        }
                    }

                    if (!String.IsNullOrEmpty(_priorityDataField))
                    {
                        string priorityValue = DataEval(o, _priorityDataField).ToString();
                        if (!String.IsNullOrEmpty(priorityValue))
                        {
                            writer.WriteElementString("priority", priorityValue);
                        }
                    }

                    writer.WriteEndElement(); // url
                }
            }
        }
Example #3
0
        public void ProcessRequest(HttpContext context)
        {
            if (!IsEnabled())
            {
                return;
            }

            List <SearchSiteMapProviderBase> searchSiteMapProviders = GetSearchSiteMapProviders();

            if (searchSiteMapProviders.Count == 0)
            {
                return;
            }

            HttpResponse response = context.Response;

            response.ContentType = "text/xml";

            //Render mode - sitemapindex or sitemap?
            if (context.Request.QueryString.Count == 0)
            {
                //sitemapindex
                String handlerName = Path.GetFileName(HttpContext.Current.Request.PhysicalPath);

                using (XmlTextWriter writer = new XmlTextWriter(response.OutputStream, Encoding.UTF8)) {
                    writer.Formatting = Formatting.Indented;
                    writer.WriteStartDocument();
                    writer.WriteStartElement("sitemapindex");
                    writer.WriteAttributeString("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9");

                    string appUrl = SearchSiteMapProviderBase.GenerateUrl(HttpContext.Current.Request.ApplicationPath + "/");

                    //Write out sitemap node in sitemapindex
                    foreach (SearchSiteMapProviderBase searchSiteMapProvider in searchSiteMapProviders)
                    {
                        writer.WriteStartElement("sitemap");

                        writer.WriteElementString("loc", appUrl + handlerName + "?sitemap=" + HttpUtility.UrlEncode(searchSiteMapProvider.Name));

                        DateTime lastModified = DateTime.UtcNow;
                        String   lastMod      = lastModified.ToString("yyyy-MM-ddThh:mm:ss.fffZ", CultureInfo.InvariantCulture);
                        writer.WriteElementString("lastmod", lastMod);

                        writer.WriteEndElement(); // sitemap
                    }

                    writer.WriteEndElement(); // sitemapindex
                    writer.WriteEndDocument();
                }
            }
            else
            {
                //sitemap
                String sitemap = context.Request.QueryString["sitemap"];

                if (String.IsNullOrEmpty(sitemap) == false)
                {
                    using (XmlTextWriter writer = new XmlTextWriter(response.OutputStream, Encoding.UTF8)) {
                        writer.Formatting = Formatting.Indented;
                        writer.WriteStartDocument();
                        writer.WriteStartElement("urlset");
                        writer.WriteAttributeString("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9");

                        foreach (SearchSiteMapProviderBase searchSiteMapProvider in _searchSiteMapProviders)
                        {
                            if (String.Compare(searchSiteMapProvider.Name, sitemap, StringComparison.Ordinal) == 0)
                            {
                                searchSiteMapProvider.WriteNodes(this, writer);
                            }
                        }

                        writer.WriteEndElement(); // urlset
                        writer.WriteEndDocument();
                    }
                }
            }
        }