Click here to Skip to main content
Licence CPOL
First Posted 2 Oct 2007
Views 29,810
Downloads 357
Bookmarked 43 times

Build a Blog Page Consuming a XML Feed

By | 2 Nov 2011 | Article
How to build a blog page using an XML feed

Screenshot - buildrssblog2.gif

Introduction

In this article, I'll show a code snippet I made in order to create a small blog using the web feed provided by another blog engine.

The Code

Let's see the snippet that retrieves the data from the feed:

protected void dataBind()
{
    //open RSS xml file

    XmlTextReader reader =
        new XmlTextReader("http://www.compranapoli.it/blog/rss.xml");

    DataSet ds = new DataSet();
    ds.ReadXml(reader); //put the rss in a dataset

    // Assigns the datset to the datagrid
    GridView1.DataSource = ds.Tables[2];

    // Binds the datagrid
    GridView1.DataBind();
}

The method dataBind() opens the file rss.xml using a XmlTextReader, puts the content in a DataSet and sets one of the tables of the dataset to the GridView I used to show the data.
This is the code I made to modify the look and feel of the GridView:

<asp:GridView ID="GridView1" runat="server" 
        AutoGenerateColumns="false" BorderWidth="0"/>
 <Columns>
   <asp:TemplateField>
     <ItemTemplate>
      <h1><%# Eval("title") %></h1>
      <hr />
      <h4><%# Eval("pubDate")%></h4><br />
      <%# Eval("description")%><br /><br />
     </ItemTemplate>
   </asp:TemplateField>
 </Columns>
</asp:GridView>

The GridView doesn't handle the paging of the XML data source by default. So, I needed to add the following method to handle the paging:

// Grid View Paging 

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    GridView1.DataBind();
}

This is the result I had calling dataBind() on page load and using an elegant CSS:

Screenshot - buildrssblog1.jpg

History

  • [2-OCT-2007]: First version of the XML parser classes released
  • [4-OCT-2007]: Added source files
  • [31-OCT-2011]: Main revision of the article, the code is the same as before

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Just Glowing



Italy Italy

Member

Follow on Twitter Follow on Twitter
Check the my Blog

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 Pinmembermanukharde9:15 2 Mar '12  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 2 Nov 2011
Article Copyright 2007 by Just Glowing
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid