Questo script mostra come effettuare il parsing di un docuemnto RSS mediante codice ASP.Net e C#.
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.Web.Security;
using System.Net;
using System.IO;
using System.Xml;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// This tutorial is provided in part by Server Intellect Web Hosting Solutions http://www.serverintellect.com
// Visit http://www.AspNetTutorials.com for more ASP.NET Tutorials
string rssURL = "http://www.codeguru.com/icom_includes/feeds/codeguru/rss-all.xml";
Response.Write("<font size=5><b>Site: " + rssURL + "</b></font><Br />");
ProcessRSSItem(rssURL);
Response.Write("<hr />");
rssURL = "http://www.developer.com/icom_includes/feeds/special/dev-5.xml";
Response.Write("<font size=5><b>Site: " + rssURL + "</b></font><Br />");
ProcessRSSItem(rssURL);
}
public void ProcessRSSItem(string rssURL)
{
WebRequest myRequest = WebRequest.Create(rssURL);
WebResponse myResponse = myRequest.GetResponse();
Stream rssStream = myResponse.GetResponseStream();
XmlDocument rssDoc = new XmlDocument();
rssDoc.Load(rssStream);
XmlNodeList rssItems = rssDoc.SelectNodes("rss/channel/item");
string title = "";
string link = "";
string description = "";
for (int i = 0; i <rssItems.Count; i++)
{
XmlNode rssDetail;
rssDetail = rssItems.Item(i).SelectSingleNode("title");
if (rssDetail != null)
{
title = rssDetail.InnerText;
}
else
{
title = "";
}
rssDetail = rssItems.Item(i).SelectSingleNode("link");
if (rssDetail != null)
{
link = rssDetail.InnerText;
}
else
{
link = "";
}
rssDetail = rssItems.Item(i).SelectSingleNode("description");
if (rssDetail != null)
{
description = rssDetail.InnerText;
}
else
{
description = "";
}
Response.Write("<p><b><a href='" + link + "' target='new'>" + title + "</a></b><br/>");
Response.Write(description + "</p>");
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>A simple RSS reader using ASP.NET 2.0 and C#</title>
<style type="text/css">
<!--
body {
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}
a:link {
color: #0000FF;
}
a:visited {
color: #0000FF;
}
a:hover {
color: #0000FF;
text-decoration: none;
}
a:active {
color: #0000FF;
}
.basix {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 11px;
}
.header1 {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 11px;
font-weight: bold;
color: #006699;
}
.lgHeader1 {
font-family: Arial, Helvetica, sans-serif;
font-size: 18px;
font-weight: bold;
color: #0066CC;
background-color: #CEE9FF;
}
-->
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
<table align="center" border="0" cellpadding="0" cellspacing="0" style="position: static"
width="752">
<tr bgcolor="#5482fc">
<td colspan="4">
<img height="1" src="/media/spacer.gif" width="1" /></td>
</tr>
<tr>
<td bgcolor="#5482fc" width="1">
<img alt="Server Intellect" height="1" src="media/spacer.gif" width="1" /></td>
<td width="250">
<a href="http://www.serverintellect.com">
<img alt="Server Intellect" border="0" height="75" src="media/logo.gif" width="250" /></a></td>
<td bgcolor="#3399ff" width="500">
<a href="http://www.serverintellect.com">
<img alt="Server Intellect" border="0" height="75" src="media/headerR1.gif" width="500" /></a></td>
<td bgcolor="#5482fc" width="1">
<img alt="Server Intellect" height="1" src="media/spacer.gif" width="1" /></td>
</tr>
<tr bgcolor="#5482fc">
<td colspan="4">
<img height="1" src="media/spacer.gif" width="1" /></td>
</tr>
</table>
<div>
<br />
<table align="center" bgcolor="#5482fc" border="0" cellpadding="5" cellspacing="1"
style="position: static" width="600">
<tr>
<td align="center" class="lgHeader1" height="50">
A simple RSS reader using ASP.NET 2.0 and C#</td>
</tr>
</table>
<br />
</div>
</div>
</form>
</body>
</html>