About the author

Name of author Kevin Brammer lives in Houston and spends most of his time painting, designing, and programming ASP.NET/C#.
E-mail me Send mail

Pages

Advertisements

 

Parse ATOM with ASP.NET

by Admin 5/4/2008 5:49:00 AM

I recently undertook the task of using ASP.NET to fetch the ATOM feed of my Google Blog and add it dynamically to my website. Surprisingly, it turned out to be pretty straightforward. I got a lot of help from from Arnaud Weil's tutorial, but I decided that I wanted to show all of the content instead of just links and titles.

I discovered very quickly that making remote web requests from a managed server was not as straight forward as I thought. After a lot of head scratching, it turns out that 1and1.com requires a web proxy be set up in the web.config file for your site in order to allow any outgoing requests.

Proxy for 1and1.com (web.config)

  <!--Get around medium trust from 1and1 shared server by using proxy for getresponse() -->
<system.net>
<defaultProxy>
<proxy usesystemdefault="False"
bypassonlocal="False"
proxyaddress="http://ntproxy.1and1.com:3128"
/>
</defaultProxy>
</system.net>

After adding the proxy to your web.config file, the next set is adding the method that will make the web request to your Page_Load event.

Web request (Default.aspx)

    protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
WebRequest r = HttpWebRequest.Create("http://cyberslinger.blogspot.com/atom.xml");
WebResponse re = r.GetResponse();
using (StreamReader sr = new StreamReader(re.GetResponseStream()))
{
Xml1.DocumentContent = sr.ReadToEnd();
}
}
}

I also set the OutputCache parameter in Default.aspx to one hour to reduce the amount of requests made for the feed.

<%@ OutputCache Duration="3600" VaryByParam="None"%>

The actual feed is rendered by the XML control, which is added somewhere in Default.aspx.

<asp:Xml ID="Xml1" runat="server" TransformSource="~/XSLTFile.xsl"></asp:Xml>

Notice that the XML control has a reference to the XSLT file that will do all the heavy lifting of transforming the Google Feed and returning the desired content to the XML control. I also had to set the output method to HTML in the XSLT file so that the output from the XML control would render properly.

XML Style Sheet (XSLTFile.xsl)

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/atom:feed">
<xsl:apply-templates select="atom:entry" />
</xsl:template>
<xsl:template match="atom:entry">
<div>
<h2>
<a href="{atom:link[@rel='alternate']/@href}"><xsl:value-of select="atom:title"/></a>
</h2>
<p>
<xsl:value-of select="atom:content" disable-output-escaping="yes"/>
</p>
<p>
<xsl:value-of select="substring(atom:published,0,11)"/>
</p>
</div>
</xsl:template>
</xsl:stylesheet>

Currently rated 1.0 by 1 people

  • Currently 1/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , , ,

ASP.NET

Related posts

Add comment


 

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Powered by BlogEngine.NET 1.3.0.0
Theme by Kevin Brammer