While creating some page for the com.dotnetclubs.Webparts project a friend faced this problem: for the RSS syndication you need to create an XML response, how would you do that with ASP.NET? As several other developers of the team also want to use RSS I guess a little explanation would do good.
Well, like in sex and Perl (or was it viceversa?), there’s more than one way to do RSS/XML response. You could actually use an HttpHandler or HttpModule to handle the response yourself. The easiest way in my opinion, however, is to part from a web service and do some tweaking. this is the way I’ll explain here
First, we want our service to generate RSS. For this the simple way will be to use XmlDocument and add the neccesary child nodes and attributes. You can find the RSS specification here. For a commented example on how to do this you can check Syndication.asmx.cs source file from the Announcements widget in com.dotnetclubs.WebParts. The basic points are:
- Remember that there are some channel elements which are required. Without them your feed won’t validate and will confuse aggregators.
- Your web method should be declared to return an XmlDocument
[code lang=”java”]
[WebMethod]
public XmlDocument GetRSS2()
{ … }
[/code]
Then we want our method response to be available from a standard browser at a URL like http://server.com/Syndication.asmx/GetRSS. That would make the browser send a request with HTTP GET verb rather than POST. Access via HTTP GET is disabled by default in ASP.NET (you may have noticed this after getting a “InvalidOperationException: Request format is unrecognized” message), so to allow our service to receive such requests we simply configure it on the configuration/system.web/webservices/protocols section of our Web.config file:
[code lang=”xml”]
[/code]
Finally we test this implementation is working using some of the external RSS validation services, like FeedValidator.org
And that’s it! Wonderful hand-made RSS ready to go. Just like this you can also create RSS0.91 or even Atom feeds.
And of course, if you don’t want to get your hands dirty, you can always use a library like RSS.NET and/or Atom.NET