Linq to Xml: Generate Google Sitemap with sitemap-protocol

by Pieter Brinkman 8. July 2009 02:22

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

[code:c#]

//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);

[/code]


This example will give the following output:

[code:xml]
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">  <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>
[/code]


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.

Tags: , , , , ,

ASP.Net | Google | Linq | XML

Convert XmlElement to XElement (Extension Method)

by Pieter Brinkman 8. December 2008 05:31

I was using some web-services that returned there values in an XmlElement. I needed to convert this XmlElement to something that I can use with LINQ. I didn't find good solutions on the internet. So I started building my own convert logic. After all kinds of solutions I ended up with creating an new XmlDocument, adding the XmlElement to the XmlDocument and then convert the innerXml of the XmlDocument to an XElement. Not really nice but it does the trick.

I've rewritten the code into a Extension Method for the XmlElement.

public static XElement ToXElement(this XmlElement xml)
{
   XmlDocument doc = new XmlDocument();

   doc.AppendChild(doc.ImportNode(xml, true));

   return XElement.Parse(doc.InnerXml);

}


You use the Extension Method like this:

XmlElement xmlElement = wsClient.DoWebServiceRequest();
XElement xml = xmlElement.ToXElement();


Please tell me if you have a better way of doing this!

Cheers,
Pieter

Tags: , , , , , , ,

Linq | XML

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen

About Me

My name is Pieter Brinkman I am Solution Architect for Sitecore in The Netherlands. My interests are mainly ASP.NET, MSSQL and Content Management Systems.

Calendar

<<  February 2012  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
2728291234
567891011

View posts in large calendar

RecentComments

Comment RSS

Most comments