Geeks With Blogs
Gaurav Taneja Great dreams... never even get out of the box. It takes an uncommon amount of guts to put your dreams on the line, to hold them up and say, "How good or how bad am I?" That's where courage comes in.

public

{

void CreateXML()

string strselect = "Your Query here";

        DataSet ds = ObjectName.ExecuteQuery(strselect, "Table Name");

 

try

        {

            string path = HttpContext.Current.Server.MapPath("rss.xml");

            XmlTextWriter writer = new XmlTextWriter(path, Encoding.UTF8);

            writer.WriteStartDocument();

            writer.WriteProcessingInstruction("xml-stylesheet", "type='text/xsl' href='lead.xsl'");

            writer.WriteStartElement("rss");

            writer.WriteAttributeString("version", "2.0");

            writer.WriteStartElement("channel");

            writer.WriteElementString("title", "Your Feed Title");

            writer.WriteElementString("link", "http://yoursite.com");

            writer.WriteElementString("description", "Your description");

            writer.WriteElementString("language", "en-us");

            string GMTDate = System.TimeZone.CurrentTimeZone.ToUniversalTime(DateTime.Now).ToString("r");

            writer.WriteElementString("lastBuildDate", "<br>" +  GMTDate);

 

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)

            {

                string strTopic = ds.Tables[0].Rows[i]["Topic"].ToString();

                string strArticle = ds.Tables[0].Rows[i]["Article"].ToString();

                string strlink = "";

                string PostDate = ds.Tables[0].Rows[i]["PostDate"].ToString();

                string listid = ds.Tables[0].Rows[i]["Id"].ToString(); ;

                strlink = "http://www.yoursite.com/index.aspx?Id=" + listid;

 

                strlink = strlink.Replace("\n", "");

                writer.WriteStartElement("item");

                writer.WriteElementString("title", strTopic);

                writer.WriteElementString("link", strlink);

                writer.WriteElementString("description", strArticle);

                writer.WriteElementString("guid", strlink);

                writer.WriteEndElement();

            }

 

            writer.WriteEndDocument();

            writer.Flush();

            writer.Close();

        }

        catch (Exception e)

        {

            throw e;

        }

}

Posted on Tuesday, March 6, 2007 7:22 AM .Net | Back to top


Comments on this post: Creating RSS feed + C#

# re: Creating RSS feed + C#
Requesting Gravatar...
Nicely done.
Concise and to the point.
Left by D Waterfield on May 04, 2009 7:09 AM

# re: Creating RSS feed + C#
Requesting Gravatar...

Hi nice code but i want to update the xml ?

visit rednews.in/blog.aspx?id=rssfed
Left by redguna on Jun 17, 2009 4:49 AM

# re: Creating RSS feed + C#
Requesting Gravatar...
nice one
Left by martin on Nov 09, 2009 1:17 AM

Your comment:
 (will show your gravatar)


Copyright © Gaurav Taneja | Powered by: GeeksWithBlogs.net