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

Load rss (xml) with Linq to XML on medium trust hosting

by Pieter Brinkman 26. November 2008 05:39

For a new project I needed to implement show data from RSS. The site will be hosted on a shared hosting environment with medium trust.

I load the rss with the following code:

XElement rssFeed = XElement.Load("[REPLACE WITH URL TO RSS]");
var posts = from item in rssFeed.Descendants("item")
                  select new
                  {
                       Title = item.Element("title").Value,
                       Description = item.Element("description").Value
                  };


This normally works fine for me... But apparently not with medium trust :-(! I got the following error.

Security Exception

Description: The application attempted to perform an operation not allowed by the security policy.  To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.

Exception Details: System.Security.SecurityException: Request for the permission of type 'System.Net.WebPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.


After reading a bit and playing with the web.config setting <TRUST /> I found the solution. The solution is putting a ".*" in the originUrl parameter of the trust node.

<trust level="Medium" originUrl=".*" />


So an easy solution for a annoying problem what took me to much time. So I hope this post will help somebody out there :-)

Tags: , , , ,

ASP.Net | Linq

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen

About Me

My name is Pieter Brinkman I am a .NET Software Engineer for Achmea IT in De Meern, The Netherlands. My interests are mainly web applications created with ASP.NET, MSSQL and Silverlight.

Calendar

<<  September 2010  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

View posts in large calendar

RecentComments

Comment RSS

Most comments

club penguin cheats club penguin cheats
4 comments
us United States
Web Design Company Web Design Company
2 comments
Web design Web design
2 comments
gb United Kingdom