Search This Blog

Monday, July 21, 2014

how to Create RSS feed Using Asp.net | RSS Feed Sample using asp.net with c#

RSS stands for Really Simple Syndication. Its allows you to syndicate your site content.

RSS is written in XML.

using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

public partial class rss : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        string connectionString = "Data Source=HP11-PC\\SQLEXPRESS;Initial Catalog=cp;Integrated Security=True";

        DataTable dt = new DataTable();
        SqlConnection conn = new SqlConnection(connectionString);
        using (conn)
        {
            SqlDataAdapter ad = new SqlDataAdapter("SELECT top 100 * from tblbuilder", conn);
            ad.Fill(dt);
        }

        Response.Clear();
        Response.ContentType = "text/xml";
        XmlTextWriter TextWriter = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
        TextWriter.WriteStartDocument();
        TextWriter.WriteStartElement("rss");
        TextWriter.WriteAttributeString("version", "2.0");
        TextWriter.WriteStartElement("channel");
        TextWriter.WriteElementString("title", "C#.NET,ASP.NET Samples and Tutorials");
        TextWriter.WriteElementString("link", "http://ask-dotnet.blogspot.in/");
        TextWriter.WriteElementString("description", "Free ASP.NET articles,C#.NET,ASP.NET tutorials and Examples,Ajax,SQL Server,Javascript,XML code examples -- by Chandra prakash");
        TextWriter.WriteElementString("copyright", "Copyright 2009 - 2014 aspdontnet.blogspot.com. All rights reserved.");
        foreach (DataRow feed in dt.Rows)
        {
            TextWriter.WriteStartElement("item");
            TextWriter.WriteElementString("name", feed["buildername"].ToString());
            TextWriter.WriteElementString("link", "http://ask-dotnet.blogspot.in//Property-" + feed["buildername"].ToString());

            TextWriter.WriteElementString("description", feed["description"].ToString());
            TextWriter.WriteEndElement();
        }
        TextWriter.WriteEndElement();
        TextWriter.WriteEndElement();
        TextWriter.WriteEndDocument();
        TextWriter.Flush();
        TextWriter.Close();
        Response.End();
    }
}


OUTPUT :

<rss version="2.0">
<channel>
<title>C#.NET,ASP.NET Samples and Tutorials</title>
<link>http://ask-dotnet.blogspot.in/</link>
<description>
Free ASP.NET articles,C#.NET,ASP.NET tutorials and Examples,Ajax,SQL Server,Javascript,XML code examples -- by Chandra prakash
</description>
<copyright>
Copyright 2009 - 2014 aspdontnet.blogspot.com. All rights reserved.
</copyright>
<item>
<name>DLF</name>
<link>http://ask-dotnet.blogspot.in//Property-DLF</link>
<description>builder name good</description>
</item>
<item>
<name>Amerpali</name>
<link>http://ask-dotnet.blogspot.in//Property-Amerpali</link>
<description>not good</description>
</item>
<item>
<name>classic</name>
<link>http://ask-dotnet.blogspot.in//Property-classic</link>
<description>very good</description>
</item>
<item>
<name>Lugoon</name>
<link>http://ask-dotnet.blogspot.in//Property-Lugoon</link>
<description>nice</description>
</item>
</channel>
</rss>

No comments :