Web Development Blog

How to parse RSS using PHP and C#

How to parse RSS using PHP and C#

In last article I explained what is RSS feed, and how to create it, this time I will show how to parse it and display on your website using PHP and C#. As we know RSS is just a XML feed that needs to be parsed meaning that the text generated in RSS has to be separated into parts like title, link, description etc…

Now I will jump right into the code without further explanations, code speaks for itself.

Parse RSS using PHP

<?
function getElement($element,$item)
{
    
$temp = split("<".$element.">",$item);
    
$temp = split("</".$element.">",$temp[1]);
    return (!empty(
$temp[0]))?trim($temp[0]):"";
}
$feedURL = "http://www.superiorwebsys.com/blog/rss/"; // should be changed to RSS that you are interested in like http://rss.cbc.ca/lineup/technology.xml
$feed=file_get_contents($feedURL);
$temp = split("</copyright>",$feed);
$temp = split("</channel>",$temp[1]);
$temp[0] = str_replace("<item>","",$temp[0]);
$temp = split("</item>",$temp[0]);
foreach(
$temp as $item)
{
    if(
getElement("pubDate",$item))
    {
?>
        <div>
        <table width="100%" cellpadding="2" cellspacing="0" border="0">
        <tr>
            <td>
                <a href="<?=getElement("link",$item)?>" style="font-family:Arial, Helvetica, sans-serif; font-size:18px; color:#993366;">
                <?=date("F d, Y",strtotime(getElement("pubDate",$item)))?> - <?=getElement("title",$item)?>
                </a>
            </td>
        </tr>
        <tr><td height="5px"></td></tr>
        <tr>
            <td style="font-family:Arial, Helvetica, sans-serif; font-size:13px; color:#2D4560;">
                <?=getElement("description",$item)?>
            </td>
        </tr>
        <tr><td height="10px"></td></tr>
        </table>
        </div>
<?
    
}
}
?>

You can see script out put here: http://superiorwebsys.com/blog/posts/rssParser.php

Parse RSS using C#

<%@ Page Language="C#" %>
<script runat="server">
    public void parse_rss(string r_url)
    {
        System.Net.WebRequest myRequest = System.Net.WebRequest.Create(r_url);
        System.Net.WebResponse myResponse = myRequest.GetResponse();

        System.IO.Stream r_stream = myResponse.GetResponseStream();
        System.Xml.XmlDocument r_doc = new System.Xml.XmlDocument();
        r_doc.Load(r_stream);

        System.Xml.XmlNodeList r_items = r_doc.SelectNodes("rss/channel/item");
        string title = "", link = "", s_description = "";

        for (int i = 0; i < r_items.Count; i++)
        {
            System.Xml.XmlNode r_detail;
			title = ""; link = ""; s_description = "";
            r_detail = r_items.Item(i).SelectSingleNode("title");
            if (r_detail != null) { title = r_detail.InnerText; }

 
            r_detail = r_items.Item(i).SelectSingleNode("link");
            if (r_detail != null) { link = r_detail.InnerText;  }

 
            r_detail = r_items.Item(i).SelectSingleNode("description");
            if (r_detail != null) { s_description = r_detail.InnerText; }


            Response.Write("<p><b><a href='" + link + "' target='new'>" + title + "</a></b><br/>" + s_description + "</p>");
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server"><%
string r_url = "http://superiorwebsys.com/blog/rss/";
parse_rss(r_url);
 %></form></body></html>

Michael Pankratov

Associated tags:  PHP, Code, RSS, XML, C

Comments:

JessicaPypes wrote on May 10, 2009 at 11:45
Very nice blog. I totally agree with your thoughts.

Arianamulps wrote on May 13, 2009 at 06:45
beautiiful blog merciiiiii

Stonss wrote on October 14, 2009 at 10:56
How hard will it be to develop apps for the iphone w/ the sdk? Do I need to be a developer to do it or just an average person with some computer knowlege?

Michael Pankratovs wrote on October 15, 2009 at 15:12

Hello,


This is nice question, not related to the topic though. It is not a rocket sconce to develop apps for Iphone, but some computer knowledge won’t help as well, there are some information on how to develop Iphone apps available on apple website. If you need professional help with Iphone applications, you can contact our company and we will be happy to help you with it!


Click here to see our contact information



KeveMivyimmugs wrote on November 9, 2009 at 07:36
I found this site using google.com. And I want to thank you for your work. You have done really very good site. Great work, great site! Thank you! Sorry for offtopic

Jacks wrote on February 16, 2010 at 08:42
www.superiorwebsys.com; You saved my day again.

Boris Myerss wrote on February 19, 2010 at 13:28
You have really great taste on catch article titles, even when you are not interested in this topic you push to read it

Bobbie Griffins wrote on February 21, 2010 at 11:31
You have tested it and writing form your personal experience or you find some information online?

Michael P.s wrote on February 22, 2010 at 09:31
We actually wrote this code from the scratch, and use it in our websites. So answering your question - yes it is tested.

Kbones wrote on March 13, 2010 at 03:42
Your blog keeps getting better and better! Your older articles are not as good as newer ones you have a lot more creativity and originality now. Keep it up! And according to this article, I totally agree with your opinion, but only this time! :)

Marks wrote on April 26, 2010 at 00:31
I bookmarked this link . Thank you for good job !

Nicks wrote on June 4, 2010 at 17:13
Thank you very much for the information great post, found it on Yahoo.

Sunnys wrote on June 5, 2010 at 11:44
Enjoyed reading the post- most informative thanks

Chriss wrote on June 9, 2010 at 06:16
very well information you write it very clean. I`m very lucky to get this information from you.

Christians wrote on June 10, 2010 at 11:10
Thank you for your great content.

Louboutins wrote on June 16, 2010 at 11:29
Really nice and impressive blog i found today.

evergeartielps wrote on July 5, 2010 at 09:37
keep up the good work

Registrys wrote on July 25, 2010 at 13:44
good share, great article, very usefull for us...thanks!

Normans wrote on September 27, 2010 at 12:16
ha, I am going to try out my thought, your post get me some good ideas, it`s truly awesome, thanks. - Norman

mallorcas wrote on October 12, 2010 at 21:16
It took me a long time to search on the net, only your site unfold the fully details, bookmarked and thanks again. - Laura

Kanes wrote on October 26, 2010 at 06:29
great read, thanks.

Gurus wrote on November 17, 2010 at 09:04
Hi guys, I’m a new member to your page, but I’ve been watching it for a little while, and the content is cool. Cheers!

Esonfemnelemos wrote on December 1, 2010 at 15:30
Very nicce!

Richs wrote on February 5, 2011 at 23:21
I seldom leave comments on blog, but I have been to this post which was recommended by my friend, lots of valuable details, thanks again.

Kenishas wrote on May 31, 2011 at 08:01
Great thinking! That rellay breaks the mold!

Add Comment:

CAPTCHA