How to parse RSS using PHP and C#

April 15, 2009

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