Hima
Hima

Reputation: 131

Creating an RSS feed in ASP.NET 3.5

How would you create an RSS feed in ASP.NET 3.5 using C#?

What framework pieces would help in making the publishing of an RSS or Atom feed easier for the .NET developer?

Are there any extra features in .NET 4 to make this task easier than in 3.5?

Upvotes: 13

Views: 7857

Answers (2)

Nick Craver
Nick Craver

Reputation: 630607

There's a new namespace for this in 3.5 called System.ServiceModel.Syndication.

Kevin Miller has a great writeup on this, not a whole lot too it with the new namespace.

Create a SyndicatedFeed filled with feed items and hand that to a Feed Formatter.

Generating an RSS feed

public void ProcessRequest(HttpContext context)
{
   SyndicationFeed feed = CreateRecentSolutionsFeed();

   var output = new StringWriter();
   var writer = new XmlTextWriter(output);

   new Rss20FeedFormatter(feed).WriteTo(writer);

   context.Response.ContentType = "application/rss+xml";
   context.Response.Write(output.ToString());
}

We have to serve this feed from somewhere. Not really being an ASP.NET expert, Josh got me started with a Generic Handler. Which seems a thin construct for simply spiting content out of a URL. Perfect. [sic]

enter image description here

A Syndication Feed

To build the syndication I created this little helper method.

private static SyndicationFeed CreateRecentSolutionsFeed()
{
   var syndicationItems = GetRecentOrModifiedSolutionSyndicationItems(TimeSpan.FromDays(30));

   return new SyndicationFeed(syndicationItems)
   {
       Title = new TextSyndicationContent("Dovetail Software Knowledge Base"),
       Description = new TextSyndicationContent("Recently created or modified solutions regarding products offered by Dovetail Software."),
       ImageUrl = new Uri("http://www.dovetailsoftware.com/images/header_logo.gif")
   };
}

Assembling a Syndication Item

I'll skip the gory Dovetail SDK data access code that goes out to the Clarify database and materializes poof any solutions that have been created or modified in the last 30 days. Unless, of course, one of my 2 readers asks nicely for it. The interesting bit in any case is that it basically loops over all the solutions found and assembles a Syndication Item for each one.

private static SyndicationItem AssembleSolutionSyndicationItem(ClarifyDataRow solution)
{
   var id = solution["id_number"].ToString();
   var title = String.Format("[{0}] {1}", id, solution["title"]);
   var content = solution["description"].ToString();
   var url = new Uri(String.Format("http://www.dovetailsoftware.com/resources/solutions/{0}.aspx", id));

   return new SyndicationItem(title, null, url) { Summary = new TextSyndicationContent(content) };
}

Results

The end result is a totally hard coded HTTP Handler generating an hopefully useful RSS feed for Dovetail’s customers.

enter image description here

Next up was to add a meta link to the feed in the HTML header of our knowledge base pages. This makes the little RSS icon shows in the address bar of honest and decent hard working web browsers.

link rel="alternate" type="application/rss+xml" href="/resources/knowledgeBaseFed.ashx" title="Dovetail Knowledgebase Feed"/

Upvotes: 14

user1826683
user1826683

Reputation: 1

public void BindData()
{                    

    StringWriter sw = new StringWriter();
    XmlTextWriter writer = new XmlTextWriter(sw);
    XmlDocument doc = new XmlDocument();
    XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);

    writer.WriteStartElement("feed");
    writer.WriteAttributeString("xmlns", "http://www.w3.org/2005/Atom");
    writer.WriteString("\n");
    writer.WriteElementString("title", this.TTT + " - " + this.Title);
    writer.WriteString("\n");


    writer.WriteStartElement("link");
    writer.WriteAttributeString("href", this.Url );
    writer.WriteEndElement();

    writer.WriteElementString("id", "urn:uuid:" + Guid.NewGuid().ToString());            
    writer.WriteElementString("updated", DateTime.UtcNow.ToString("o"));


    foreach (var  item in this.lista)
    {
        writer.WriteStartElement("entry");
        writer.WriteElementString("title", item.Value.Title);

        writer.WriteStartElement("link");
        writer.WriteAttributeString("href", item.Key);
        writer.WriteEndElement();

        writer.WriteElementString("id", item.Key);
        string slikaImgUrl = item.Value.Imaga;

        if (string.IsNullOrEmpty(slikaImgUrl) == false)
        {
            writer.WriteStartElement("link");
            writer.WriteAttributeString("rel", "enclosure");
            writer.WriteAttributeString("type", "image/jpeg");
            writer.WriteAttributeString("href", slikaImgUrl);
            writer.WriteEndElement();
        }

        writer.WriteStartElement("author");
        writer.WriteElementString("name", this.Title);
        writer.WriteEndElement();

        writer.WriteStartElement("summary");
        writer.WriteAttributeString("type", "text");
        writer.WriteCData(" ");
        writer.WriteEndElement();               

        writer.WriteElementString("updated", DateTime.UtcNow.ToString("o"));
        writer.WriteElementString("published", DateTime.UtcNow.ToString("o"));
        writer.WriteEndElement();
        writer.WriteString("\n");                
    }

    writer.WriteEndElement();
    string dataOut = sw.ToString();


    Response.Clear();
    Response.ContentType = "text/xml";

    Response.Write(dataOut);
    writer.Close();
    Response.End();
}

Upvotes: -1

Related Questions