Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RSS Item Limits

Status
Not open for further replies.

elhaix

Programmer
Jul 31, 2006
115
CA
I'm reading an RSS feed via:

Code:
    protected void Page_Load(object sender, EventArgs e)
    {
        recentPosts.DataSource = GetRSSFeed("[URL unfurl="true"]http://news.google.com/news?hl=en&ned=us&ie=UTF-8&q=Living+room+entertainment+furniture&output=rss");[/URL]
        recentPosts.DataBind();

        //Response.Write(GetRSSFeed("[URL unfurl="true"]http://www.aspmessageboard.com/scripts/rss.xml"));[/URL]
    }

    protected DataTable GetRSSFeed(string strURL)
    {
        // get the XML data
        XmlTextReader reader = new XmlTextReader(strURL);
           
        // return a new DataSet
        DataSet ds = new DataSet();
        ds.ReadXml(reader);
        return ds.Tables[3];
    }

This works great, and I can see all the feed items in my DG. However, I want to limit the number of <items> (ie. to three rows) - and not use DG paging. I thought about just setting paging=true, set the # to 3 and hiding, through CSS, the paging field (display: none), but that's ugly.

So I figured I'd iterate through the reader, create a new XmlTextWriter, and omit any <item></item>'s who's count is > my max item count - ie. 3.

Can someone lend a hand on this one please, or recommend a better way to do this?

Thanks!
 
Solution found.

To recap: Reading an RSS feed, data-binding to a DataGrid/other data control, and wanting to limit the number of RSS items to be displayed.

The solution here was to:
1) create a DataView of the DataSet.
2) create a new DataTable, where we will copy desired DataView rows to
3) iterate through the DV, copy rows until row-count is reached


Code:
    const string RSS_FEED = "[URL unfurl="true"]http://news.google.com/news?hl=en&ned=us&ie=UTF-8&q=Living+room+entertainment+furniture&output=rss";[/URL]
    const int MAX_RSS_ROWS = 2;

    protected void Page_Load(object sender, EventArgs e)
    {
        recentPosts.DataSource = GetRSSFeed(RSS_FEED);
        recentPosts.DataBind();
    }



    protected DataTable GetRSSFeed(string strURL)
    {
        XmlTextReader reader = new XmlTextReader(strURL);

        DataSet ds = new DataSet();
        ds.ReadXml(reader);         // populate dataset
        
        // populate dv so we can filter out extra <item> rows
        DataView dv = new DataView(ds.Tables[3]);
        DataTable outputTable = dv.Table.Clone();
        
        // iterate through the dv rows, copy them to the outputTable until max rows is reached
        for (int x = 0; x < MAX_RSS_ROWS; x++)
        {
            outputTable.ImportRow(dv.Table.Rows[x]);
        }

        return outputTable;
    }

Incidentally, if you want to grab the entire code from an HTML page:

Code:
    /// <summary>
    /// Reads XML from a given URL
    /// </summary>
    /// <param name="sourceFile"></param>
    /// <returns></returns>
    protected object getXML(string sourceFile)
    {
        System.Net.WebRequest myRequest = System.Net.WebRequest.Create(sourceFile);
        System.Net.WebResponse myResponse = myRequest.GetResponse();
        System.Xml.XmlTextReader myReader = new System.Xml.XmlTextReader(myResponse.GetResponseStream());
        System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
        doc.Load(myReader);
        return (doc);
    }



I hope this solution can help you guys.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top