Linq to Xml: Generate Google Sitemap with sitemap-protocol

VN:F [1.9.22_1171]
Rating: 10.0/10 (1 vote cast)

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.

VN:F [1.9.22_1171]
Rating: 10.0/10 (1 vote cast)
Linq to Xml: Generate Google Sitemap with sitemap-protocol, 10.0 out of 10 based on 1 rating
Linq to Xml: Generate Google Sitemap with sitemap protocol Pieter Brinkman avatarAuthor: Pieter Brinkman ()

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+

Linq to Xml: Generate Google Sitemap with sitemap protocol WebsiteLinq to Xml: Generate Google Sitemap with sitemap protocol TwitterLinq to Xml: Generate Google Sitemap with sitemap protocol Google PlusLinq to Xml: Generate Google Sitemap with sitemap protocol Linkedin

Related posts

C#: Get Parent Control with Generics
LINQ: Creating a if statement in Linq query
WCF webservice error with DBML objects
Run website on IIS7 (Vista) in classic mode

One Response to Linq to Xml: Generate Google Sitemap with sitemap-protocol

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

    Click on a tab to select how you'd like to leave your comment

    Leave a Reply

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

    *

    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>