Skip to content

Instantly share code, notes, and snippets.

@warrenbuckley
Created July 31, 2013 09:15
Show Gist options
  • Save warrenbuckley/6120612 to your computer and use it in GitHub Desktop.
Save warrenbuckley/6120612 to your computer and use it in GitHub Desktop.
Trying to create an XML Sitemap in Umbraco 6. Getting the following error: ============================================================ This page contains the following errors: error on line 9 at column 10: Extra content at the end of the document Below is a rendering of the page up to the first error.
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@{
Layout = null;
Response.ContentType = "text/xml";
}<xml version="1.0" encoding="UTF-8">
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>http://www.example.com/</loc>
<lastmod>2005-01-01</lastmod>
<changefreq>monthly</changefreq>
<priority>0.8</priority>
</url>
</urlset>
@warrenbuckley
Copy link
Author

Resolved with this

@using System.Text
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@{
    Layout = null;

    Response.Clear();
    Response.ContentType        = "text/xml";
    Response.ContentEncoding    = Encoding.UTF8;

}<?xml version="1.0" encoding="UTF-8" ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url>
        <loc>http://www.example.com/</loc>
        <lastmod>2005-01-01</lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.8</priority>
    </url>
</urlset>

@abjerner
Copy link

Damn. You beat me to it. But as you probably found out, it was the XML "header" missing the question marks.

Anyways, for XML generation, XDocument and XElement might be good friends ;)

XDocument doc = new XDocument(
    new XDeclaration("1.0", "utf-8", "yes"),
    new XElement(
        "urlset", 
        new XElement(
            "url",
            new XElement("loc", "http://www.example.com/"),
            new XElement("lastmod", "2005-01-01"),
            new XElement("changefreq", "monthly"),
            new XElement("priority", "0.8")
        )
    )
);

@warrenbuckley
Copy link
Author

OK this is a more polished result now:

@using System.Text
@using umbraco.MacroEngines
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@{
    Layout = null;

    //Set to XML
    Response.ContentType = "text/xml";

    //Homepage node
    var homepage = Umbraco.TypedContentAtRoot().SingleOrDefault();

    //Base URL
    var baseUrl = homepage.UrlWithDomain();
    baseUrl     = baseUrl.EndsWith("/") ? baseUrl.TrimEnd("/") : baseUrl;

    //Get child pages of homepage
    var naviPages = homepage.Children.Where(x => x.IsVisible());

}<?xml version="1.0" encoding="UTF-8" ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    @ListChildNodes(naviPages, baseUrl)
</urlset>

@helper ListChildNodes(IEnumerable<IPublishedContent> naviItems, string baseUrl)
{
    //For each page in the collection...
    foreach (var item in naviItems)
    {
       <url>
            <loc>@baseUrl@item.Url</loc>
            <lastmod>@item.UpdateDate.ToString("yyyy-MM-dd")</lastmod>
            <changefreq>monthly</changefreq>
            <priority>0.8</priority>
        </url>

        @* If the current node in the loop has children *@
        if(item.Children.Any())
        {
            @* Re-run the helper with the children of the current node *@
            @ListChildNodes(item.Children, baseUrl)
        }
    }
}

@drift
Copy link

drift commented Aug 2, 2013

nice, just a heads up:

 var homepage = Umbraco.TypedContentAtRoot().SingleOrDefault();

will fail for multiple sites

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment