Fun Infused Games  |   Smooth Operator  |   Evil Scale  |   Starcraft Live  |   Wellplayed.net RSS Feed Available RSS 

  Home   |    Archive   |    Subscribe   |    Search   |    About

Posts prior to 8/2/2010 may be missing data. If you need one of those posts, please contact kriswd40@yahoo.com and I will try and recover/find it.

A Simple RSS Feed using XmlTextWriter
Date 7/15/2008    Tags ASP.net, C#    (4)

RSS is a beautiful thing. No website should be without at least a basic RSS feed. For many of my sites, I use a CreateXML() function that will write an XML file (using the XMLTextWriter) which allows you to share your latest news with the world. Since most of my sites only have occassional updates (like this blog for example, which is rarely updated more than once a day), I like to setup the CreateXML() function to run anytime and update is made to one of my blogs (insert, update, delete).

Below is the code that I use. It's pretty basic, but it creates a nice RSS feed that is pretty easy to integrate into your site.

    private void CreateXML()
    {

        String CurrentDate = GetRfc822Date(DateTime.Now);

        XmlTextWriter writer = new XmlTextWriter(HttpContext.Current.Server.MapPath("/myfeed.xml"), 

null);

        writer.WriteStartDocument();

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

        writer.WriteStartElement("channel");

        // Start of channel items

        writer.WriteStartElement("title");
        writer.WriteString("KrisSteele.net");
        writer.WriteEndElement();

        writer.WriteStartElement("link");
        writer.WriteString("http://www.krissteele.net/");
        writer.WriteEndElement();

        writer.WriteStartElement("description");
        writer.WriteString("The official blog of Kris Steele");
        writer.WriteEndElement();

        writer.WriteStartElement("language");
        writer.WriteString("en-us");
        writer.WriteEndElement();

        writer.WriteStartElement("pubDate");
        writer.WriteString(CurrentDate);
        writer.WriteEndElement();

        writer.WriteStartElement("lastBuildDate");
        writer.WriteString(CurrentDate);
        writer.WriteEndElement();

        writer.WriteStartElement("docs");
        writer.WriteString("http://www.krissteele.net");
        writer.WriteEndElement();

        writer.WriteStartElement("generator");
        writer.WriteString("ASP.net XML Writer");
        writer.WriteEndElement();

        writer.WriteStartElement("managingEditor");
        writer.WriteString("kriswd40@yahoo.com (Kris Steele)");
        writer.WriteEndElement();

        writer.WriteStartElement("webMaster");
        writer.WriteString("kriswd40@yahoo.com (Kris Steele)");
        writer.WriteEndElement();


        string sqlString = "SELECT TOP 10 * FROM MyBlog ORDER BY BlogID DESC";
        int loopCount = 0;


            SqlConnection MyConnection = new SqlConnection("WouldntYouLikeToKnowThisInfo");
            SqlCommand objCmd = new SqlCommand(sqlString, MyConnection);
            objCmd.Connection.Open();
            SqlDataReader MyReader;
            MyReader = objCmd.ExecuteReader(CommandBehavior.CloseConnection);

            while (MyReader.Read())
            {

                writer.WriteStartElement(Wellplayed.net
                    
                    
                    

                    
This article has been view 25 times.

Advertisement:

Comments

Derik Whittaker

Avatar

7/16/2008 4:38:07 AM

This is a nice approach, but this forces you to know the RSS layout. If you want a real simple approach use the Argotic RSS API. You can get more info on that here -- http://dimecasts.net/Casts/CastDetails/19

Rock on
shamveel

Avatar

11/18/2008 11:26:32 PM

how do u add an image to the rss like: yahoo groups in this approachu
Balaji

Avatar

2/4/2009 11:40:32 PM

Nothingispl
wesley

Avatar

4/27/2010 10:12:19 AM

Hi,

How can i get these files: System.Xml and System.Data.SqlClient in your project or .cs file.

In your xml code where is the place for database connection?

SqlConnection MyConnection = new SqlConnection("DSN=bella;UID=finding;PWD=fd142;Database=bellafin");
SqlCommand objCmd = new SqlCommand(sqlString, MyConnection);
objCmd.Connection.Open();
SqlDataReader MyReader;
MyReader = objCmd.ExecuteReader(CommandBehavior.CloseConnection);

I replace to my sql connection, is it right?

Thanks,
Wesley.


Add Comments

Current disabled. Check back soon!
top
top


top
Tags
ASP.net (17)  Annoyances (4)  Video Games (6)  Sage-Like Advice (12)  Domain Name (1)  Internet (5)  NFL (2)  Writing (1)  Visual Studio (1)  Hypership (12)  Site News (2)  Xbox (1)  C# (15)  Sage-Like Advise (1)  Education (1)  Tech Support (1)  MSSQL (1)  Absurd (1)  Abduction Action! (27)  Nasty (36)  Economy (1)  Cool (2)  Sports (11)  .Net (1)  Web Tools (2)  Abduction! (1)  Rant (40)  XBLIG (20)  Tutorial (2)  Nastier (1)  Books (1)  Realty (1)  Programming (3)  Weird (1)  Vista (1)  Development (14)  Design (3)  XNA (35)  Links (1)  Security (2)  JavaScript (7)  CSS (1)  Video (2)  Web Development (1)  Computing (1)  Abudction Action! (1)  Popularity (1)  Game Design (4)  Google (1)  AJAX (4)  Game Development (8)  
top

top
top


Twitter

    Follow me on Twitter



    Buy My Games:
    Abduction Action!
    Nasty

    I'm with Coco.