Quantcast
Channel: Sitemap doesn't show as xml - Stack Overflow
Viewing all articles
Browse latest Browse all 3

Answer by radu florescu for Sitemap doesn't show as xml

$
0
0

EDIT 1:

It seems that you may lose some of the xml specific header files:

Check this link out

This might also be a valid example on how to create an xml site map:

Sitemap xml for googleAnother example
OPTION1Try this code out(taken from here):

    [WebService(Namespace = "http://tempuri.org/")]    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]    public class Sitemap : IHttpHandler    {        private const string NAMESPACE = "http://www.sitemaps.org/schemas/sitemap/0.9";        public void ProcessRequest(HttpContext context)        {            context.Response.ContentType = "text/xml";            XmlDocument sitemapDocument = GetSitemapDocument();            context.Response.Write(sitemapDocument.InnerXml);        }        #region Build sitemap document methods        private XmlDocument GetSitemapDocument()        {            XmlDocument sitemapDocument = new XmlDocument();            sitemapDocument.PreserveWhitespace = true;            XmlDeclaration xmlDeclaration =                 sitemapDocument.CreateXmlDeclaration("1.0", "UTF-8", string.Empty);            sitemapDocument.AppendChild(xmlDeclaration);            XmlElement urlset = sitemapDocument.CreateElement("urlset", NAMESPACE);            sitemapDocument.AppendChild(urlset);            List<SitemapPage> urls = GetSitemapPages();            foreach (SitemapPage sitemapPage in urls)            {                XmlElement url = CreateUrlElement(sitemapDocument, sitemapPage);                urlset.AppendChild(url);            }            return sitemapDocument;        }        private XmlElement CreateUrlElement(XmlDocument sitemapDocument,             SitemapPage sitemapPage)        {            XmlElement url = sitemapDocument.CreateElement("url", NAMESPACE);            XmlElement loc = CreateElementWithText(sitemapDocument, "loc",                 sitemapPage.Location);            url.AppendChild(loc);            if (sitemapPage.LastModificationDate.HasValue)            {                //lastmod must be a string that comforms to the W3C Datetime format                string lastModValue = sitemapPage.LastModificationDate.Value.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ssK");                XmlElement lastmod = CreateElementWithText(                    sitemapDocument, "lastmod", lastModValue);                url.AppendChild(lastmod);            }            if (!string.IsNullOrEmpty(sitemapPage.ChangeFrequency))            {                XmlElement changefreq = CreateElementWithText(sitemapDocument, "changefreq", sitemapPage.ChangeFrequency);                url.AppendChild(changefreq);            }            if (sitemapPage.Priority.HasValue)            {                XmlElement priority = CreateElementWithText(sitemapDocument,"priority", sitemapPage.Priority.Value.ToString(                        CultureInfo.CreateSpecificCulture("en-US")));                url.AppendChild(priority);            }            return url;        }        private XmlElement CreateElementWithText(            XmlDocument document, string elementName, string text)        {            XmlElement element = document.CreateElement(elementName, NAMESPACE);            XmlText elementValue = document.CreateTextNode(text);            element.AppendChild(elementValue);            return element;        }        #endregion        private List<SitemapPage> GetSitemapPages()        {            List<SitemapPage> sitemapPages = new List<SitemapPage>();            //Example implementation            sitemapPages.Add(new SitemapPage("http://www.mydomain.com")                 { ChangeFrequency = "daily", LastModificationDate = DateTime.Now, Priority = 1f });            sitemapPages.Add(new SitemapPage("http://www.mydomain.com/aPage.aspx")                 { ChangeFrequency = "daily", LastModificationDate = DateTime.Now, Priority = 0.8f });            return sitemapPages;        }        private class SitemapPage        {            public SitemapPage(string location)            {                Location = location;            }            public string Location { get; private set; }            public DateTime? LastModificationDate { get; set; }            public string ChangeFrequency { get; set; }            public float? Priority { get; set; }        }        public bool IsReusable        {            get            {                return true;            }        }    }

OPTION2: According to MSDN this is a valid sitemap: sitemap MSDN.

Check this output out:

<siteMap><siteMapNode title="Home" description="Home" url="~/default.aspx"><siteMapNode title="Products" description="Our products"      url="~/Products.aspx"><siteMapNode title="Hardware" description="Hardware choices"        url="~/Hardware.aspx" /><siteMapNode title="Software" description="Software choices"        url="~/Software.aspx" /></siteMapNode><siteMapNode title="Services" description="Services we offer"        url="~/Services.aspx"><siteMapNode title="Training" description="Training classes"          url="~/Training.aspx" /><siteMapNode title="Consulting" description="Consulting services"          url="~/Consulting.aspx" /><siteMapNode title="Support" description="Supports plans"          url="~/Support.aspx" /></siteMapNode></siteMapNode></siteMap>

Maybe you should try change your code:

public class XmlSitemapResult : ActionResult{    private XNamespace nsSitemap = "http://www.sitemaps.org/schemas/sitemap/0.9";    private XNamespace nsXhtml = "http://www.w3.org/1999/xhtml";    private IEnumerable<SitemapItem> _items;    public XmlSitemapResult(IEnumerable<SitemapItem> items)    {        _items = items;    }    public override void ExecuteResult(ControllerContext context)    {        string encoding = context.HttpContext.Response.ContentEncoding.WebName;        XDocument sitemap = new XDocument(new XDeclaration("1.0", encoding, "yes"),             new XElement(nsSitemap +"siteMap", new XAttribute(XNamespace.Xmlns +"xhtml", nsXhtml),                  from item in _items                  select CreateItemElement(item)                  )             );        context.HttpContext.Response.ContentType = "application/xml";        context.HttpContext.Response.Charset = encoding;        context.HttpContext.Response.Flush();        context.HttpContext.Response.Write(sitemap.Declaration + sitemap.ToString());    }    private XElement CreateItemElement(SitemapItem item)    {        XElement itemElement = new XElement(nsSitemap +"siteMapNode", new XElement(nsSitemap +"loc", item.Url.ToLower()));        if (item.LastModified.HasValue)            itemElement.Add(new XElement(nsSitemap +"lastmod", item.LastModified.Value.ToString("yyyy-MM-dd")));        if (item.ChangeFrequency.HasValue)            itemElement.Add(new XElement(nsSitemap +"changefreq", item.ChangeFrequency.Value.ToString().ToLower()));        if (item.Priority.HasValue)            itemElement.Add(new XElement(nsSitemap +"priority", item.Priority.Value.ToString(CultureInfo.InvariantCulture)));        foreach (var alternateLink in item.AlternateLinks)        {            itemElement.Add(new XElement(nsXhtml +"link",                 new XAttribute("rel", "alternate"),                new XAttribute("hreflang", alternateLink.Language),                new XAttribute("href", alternateLink.Url)));        }        return itemElement;    }}

Viewing all articles
Browse latest Browse all 3

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>