all things Sitecore related

Linq to Xml: Generate Google Sitemap with sitemap-protocol

By on July 8, 2009 in ASP.Net, Google, linq, XML with 1 Comment

In this example I will generate a XML site-map that complies with the sitemap-protocol XML schema.


//create datasource

List<string> blogPosts = new List<string>{
 "http://blog.newguid.net/mypost1.aspx",
 "http://blog.newguid.net/mypost_about_Net.aspx",
 "http://blog.newguid.net/morePosts.aspx",
 "http://blog.newguid.net/andEvenMorePosts.aspx"
};

//Create namespace for sitemap-protocol

XNamespace xmlNS = "http://www.sitemaps.org/schemas/sitemap/0.9";
XDocument xmlDoc =
 new XDocument(
  new XDeclaration("1.0", "UTF-8", null),
  new XElement(xmlNS + "urlset",
   from blogPostUrl in blogPosts
   select
    new XElement(xmlNS + "url",
    new XElement(xmlNS + "loc", blogPostUrl))
    ));

//Show output

Response.Write(xmlDoc);


This example will give the following output:


<urlset xmlns="<a href="http://www.sitemaps.org/schemas/sitemap/0.9">http://www.sitemaps.org/schemas/sitemap/0.9</a>">
  <url>
    <loc>http://blog.newguid.net/mypost1.aspx</loc>
  </url>
  <url>
    <loc>http://blog.newguid.net/mypost_about_Net.aspx</loc>
  </url>
  <url>
    <loc>http://blog.newguid.net/morePosts.aspx</loc>
  </url>
  <url>
    <loc>http://blog.newguid.net/andEvenMorePosts.aspx</loc>
  </url>
</urlset>


To keep the example as simple as possible I only use the LOC element of the URL node. In the real world you can implement the lastmod, changefreq and priority node.

More information about the sitemap-protocol.

About the Author

About the Author: Pieter is Technical Marketing Manager for Sitecore Netherlands and owner of Gaatverweg.nl. He has more than ten years experience with software developing in multiple programming languages and with different Content Management Systems. Before joining Sitecore Pieter was a lead developer for multiple Sitecore and .Net projects, he joined Sitecore in 2011 as an Solution Architect in The Netherlands, after two years as an Solution Architect he joined the Technical Marketing department. In the role as Techinical Marketing Manager he is responsible for the Global MVP program and the Sitecore technical branding strategy. You can follow Pieter on twitter: @pieterbrink123 or Google+ .

Subscribe

If you enjoyed this article, subscribe now to receive more just like it.

There is 1 Brilliant Comment

Trackback URL | Comments RSS Feed

  1. That’s great, I never thought about Generate Google Sitemap with sitemap-protocol like that before.

Post a Comment

Your email address will not be published. Required fields are marked *

Top