Friday, April 29, 2011

How to fill RSS feeds in datagrid ?

Hello Friends,

I am using .net c# and i want to fill datagrid with rss feed.

Problem is

When i return rss feed in to dataset then i got multiple table wich storing diffrent data.

Means i want to fill grid with "Title" and "Picture" here is my code example

protected void Button1_Click(object sender, System.EventArgs e)
{
   XmlTextReader reader = new XmlTextReader(txtUrl.Text); 
   DataSet ds = new DataSet();
   ds.ReadXml(reader);
   myDataGrid.DataSource = ds.Tables[2] ; 
   myDataGrid.DataBind(); 
}

All the details regarding Title and description and posted date is sitting in tables #2 and related image and size of image related information is stored in table #3 so how can i feel grid with this two column ?

Thanks in advance

From stackoverflow
  • I recommend you try the RSS Tookit. It's open source and handles RSS feed nicely. You can easily bind to a RSS feed like this:

    <ast:RssDataSource id="RssDataSource1" runat="server" maxitems="5" url="http://news.google.com/?output=atom"></ast:RssDataSource>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="RssDataSource1">
      <Columns>
        <asp:HyperLinkField DataNavigateUrlFields="link" DataTextField="link" HeaderText="Link"/>
        <asp:BoundField DataField="title" HeaderText="Title" SortExpression="title" />
      </Columns>
    </asp:GridView>
    

    Source taken from this page.

  • Or, you could just use the in-built XmlDataSource.

    <asp:XmlDataSource id="XmlDataSource1" runat="server" XPath="/channel/item" DataFile="rssFeed.xml" />
    
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="XmlDataSource1">
      <Columns>
        <asp:BoundField DataField="title" HeaderText="Title" SortExpression="title" />
        <asp:BoundField DataField="description" HeaderText="Description" />
      </Columns>
    </asp:GridView>
    

    I know the example I've given is for a GridView, but I think the same principle applies to DataGrids. The XPath expression assumes RSS 2.0.

0 comments:

Post a Comment