HOME CLIENTS SERVICES BLOG ABOUT US CONTACT US
Categories:
 Internet Marketing
 IT Humour
 IT Industry News
 Search Engines
 User Guides
 Web Development
Browse by tags
Search Blog:
RSS
Subscribe to our newsletter
Name*:
E-Mail*:
Recent Posts
iPad /iPhone Applications Development
Generate QR Code with PHP Using Google API
Most Common Connection Speeds in 2009
How to change design of Moneris Hosted Paypage
Most Common Screen Resolutions in 2009
Running Flowplayer (Flash Player) on local computer
April 15, 2009 - 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:
JessicaPype wrote on May 10, 2009 at 11:45
Very nice blog. I totally agree with your thoughts.
Arianamulp wrote on May 13, 2009 at 06:45
beautiiful blog merciiiiii
Stons 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 Pankratov 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

KeveMivyimmug 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
Jack wrote on February 16, 2010 at 08:42
www.superiorwebsys.com; You saved my day again.
Boris Myers 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 Griffin 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. 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.
Kbone 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! :)
Mark wrote on April 26, 2010 at 00:31
I bookmarked this link . Thank you for good job !
Nick wrote on June 4, 2010 at 17:13
Thank you very much for the information great post, found it on Yahoo.
Sunny wrote on June 5, 2010 at 11:44
Enjoyed reading the post- most informative thanks
Chris 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.
Christian wrote on June 10, 2010 at 11:10
Thank you for your great content.
Louboutin wrote on June 16, 2010 at 11:29
Really nice and impressive blog i found today.
evergeartielp wrote on July 5, 2010 at 09:37
keep up the good work
Registry wrote on July 25, 2010 at 13:44
good share, great article, very usefull for us...thanks!
Add Comment:
Name*:
E-Mail: Website:
Your message*:
© 2000 - 2010 Superior Web Solutions   Web design    Web development    Online marketing