Friday, November 14, 2008

Create Rss Feed (Web service) using C# Asp.net Access database

Creating your own webservice (RSS feed) with access/mysql/sql-server database. following is the full code, where you can see how to create a rss feed using asp.net c# access database. first you have to create a project in visual studios 2005 as web service (.ashx) and use following code to build your feed. (live sample http://visli.com/rss.ashx)


<%@ WebHandler Language="C#" Class="rss" %>

using System;
using System.Web;
using System.Data.OleDb;
using System.Text;
using System.Xml;

public class rss : IHttpHandler {

    public void ProcessRequest(HttpContext context)
    {

        string sTxt = BuildXmlString();
        context.Response.ContentType = "text/xml";
        context.Response.ContentEncoding = System.Text.Encoding.UTF8;
        context.Response.Write(sTxt);
    }
        
  private string BuildXmlString()
    {
        string sTitle = "Super Value Deals and Deals of The Day";
        string sSiteUrl = "http://www.visli.com";
        string sDescription = "Super Value Deals and Deals of The Day";
        string sTTL = "60";

        System.Text.StringBuilder oBuilder = new System.Text.StringBuilder();
        oBuilder.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        oBuilder.Append("<rss version=\"2.0\"><channel>");

        oBuilder.Append("<title>");
        oBuilder.Append(sTitle);
        oBuilder.Append("</title>");

        oBuilder.Append("<link>");
        oBuilder.Append(sSiteUrl);
        oBuilder.Append("</link>");

        oBuilder.Append("<description>");
        oBuilder.Append(sDescription);
        oBuilder.Append("</description>");

        oBuilder.Append("<ttl>");
        oBuilder.Append(sTTL);
        oBuilder.Append("</ttl>");


        AppendItems(oBuilder);


        oBuilder.Append("</channel></rss>");
        return oBuilder.ToString();
    }


    public void AppendItems(System.Text.StringBuilder oBuilder)
    {

        string connectionString = "provider=Microsoft.Jet.OLEDB.4.0;data source=c:\\xxxx\\database\\xxxxDB.mdb";
        OleDbConnection myOleDbConnection = new OleDbConnection(connectionString);
        OleDbCommand myOleDbCommand = myOleDbConnection.CreateCommand();
        string ProdTy = "M2";
        string Strsql = "SELECT  top 50 * from DetailUrl where ProdType='M2'";

        myOleDbCommand.CommandText = Strsql;
        myOleDbConnection.Open();

        OleDbDataReader myOleDbDataReader = myOleDbCommand.ExecuteReader();

        myOleDbDataReader.Read();
        while (myOleDbDataReader.Read())
        { 
        
        
            string sTitle = "Super Value Deals and Deals of The Day";
            string sLink = "http://www.visli.com/";
            string sDescription = "Super Value Deals and Deals of The Day";
            string sPubDate = "System.DateTime.Now";

            oBuilder.Append("<item>");

            oBuilder.Append("<title>");
            oBuilder.Append(myOleDbDataReader["TextUrl"].ToString());
            oBuilder.Append("</title>");

            oBuilder.Append("<link>");
            oBuilder.Append(myOleDbDataReader["NameUrl"].ToString());
            oBuilder.Append("</link>");
            
            oBuilder.Append("<description>");
            oBuilder.Append("&lt;img src=" + myOleDbDataReader["ImageUrl"].ToString() + "&gt;" + "&lt;br&gt;" + myOleDbDataReader["Price"].ToString() + "&lt;br&gt;visit  &lt;a href=http://www.visli.com&gt;http://www.visli.com&lt;/a&gt;");
            oBuilder.Append("</description>");

            oBuilder.Append("<pubDate>");
            oBuilder.Append(myOleDbDataReader["UpdDate"].ToString());
            oBuilder.Append("</pubDate>");

            oBuilder.Append("</item>");


        }
    }


    
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}

2 comments:

Noel Ablon said...

Thanks! I'll try this out as soon as I finish my project.

Tony said...

I am looking for someone that I can hire to fix my blog RSS feed e.g.
How can I reduce Blogger's feed size below FeedBurner's 512K limit?

My problem is that when I attempt to follow the directions I cannot access the feed management page because it doesn't appear so I cannot edit my feed details.