Linq to Xml: Generate Google Sitemap with sitemap-protocol

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

//create datasource

List blogPosts = new List{

//Create namespace for sitemap-protocol

XNamespace xmlNS = "";
XDocument xmlDoc =
 new XDocument(
  new XDeclaration("1.0", "UTF-8", null),
  new XElement(xmlNS + "urlset",
   from blogPostUrl in blogPosts
    new XElement(xmlNS + "url",
    new XElement(xmlNS + "loc", blogPostUrl))

//Show output


This example will give the following output:

&lt;urlset xmlns=&quot;<a href=""></a>"&gt;


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.

One Reply to “Linq to Xml: Generate Google Sitemap with sitemap-protocol”

Leave a Reply

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