Quantcast
Viewing all articles
Browse latest Browse all 3

Sitemap doesn't show as xml

I've got the following classes for my sitemap generation:

public class SitemapItem{    public SitemapItem(string url)    {        this.Url = url;        this.AlternateLinks = new List<SiteMapAlternateLink>();    }    public string Url { get; set; }    public DateTime? LastModified { get; set; }    public ChangeFrequency? ChangeFrequency { get; set; }    public float? Priority { get; set; }    public List<SiteMapAlternateLink> AlternateLinks { get; set; }}

And:

public class SiteMapAlternateLink{    public SiteMapAlternateLink(string url, string language)    {        this.Url = url;        this.Language = language;    }    public string Url { get; set; }    public string Language { get; set; }}

Now in my controller I fill a list of SitemapItems and return it from the controller with the following 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 +"urlset", 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 +"url", 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;    }}

Now the problem is that in my browser I won't see the XML as XML. I'll just see text which doesn't get viewed with the standard XML Viewer. This happens in all browsers and it seems it happend from the moment I added the xhtml schema.

Hopefully somebody sees the problem, thanks in advance!

EDIT: If I remove everything that has to do with xhtml the browser DOES show it as a xml. Any ideas?

EDIT2: The html:

<urlset xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><url><loc>http://localhost:11149/en</loc><changefreq>hourly</changefreq><priority>0.6</priority><xhtml:link rel="alternate" hreflang="en" href="http://localhost:11149/en"/><xhtml:link rel="alternate" hreflang="nl" href="http://localhost:11149/nl"/></url><url><loc>http://localhost:11149/en/buyandsell</loc><changefreq>weekly</changefreq><priority>1</priority><xhtml:link rel="alternate" hreflang="en" href="http://localhost:11149/en/BuyAndSell"/><xhtml:link rel="alternate" hreflang="nl" href="http://localhost:11149/nl/BuyAndSell"/></url><url><loc>http://localhost:11149/en/partner</loc><changefreq>weekly</changefreq><priority>0.5</priority><xhtml:link rel="alternate" hreflang="en" href="http://localhost:11149/en/Partner"/><xhtml:link rel="alternate" hreflang="nl" href="http://localhost:11149/nl/Partner"/></url><url><loc>http://localhost:11149/en/news</loc><lastmod>2013-12-06</lastmod><changefreq>daily</changefreq><priority>0.6</priority><xhtml:link rel="alternate" hreflang="en" href="http://localhost:11149/en/News"/><xhtml:link rel="alternate" hreflang="nl" href="http://localhost:11149/nl/News"/></url></urlset>

Viewing all articles
Browse latest Browse all 3

Trending Articles