现实简单的RSS订阅功能,对RSS feeds的分析

来源:互联网 发布:flvcd视频下载软件 编辑:程序博客网 时间:2024/05/01 23:26

/*

 

以下现实简单的RSS订阅功能,参照网上资料,改写!

<!--Page Language="C#"--><!--Import Namespace="System.Xml"--><!--Import Namespace="System.Net"--><!--CTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt--><script runat="server">

  public string ProcessRSSItem(string rssURL,bool bWrite)
  {
   string tmpRet="";
   
   System.Net.WebRequest myRequest=System.Net.WebRequest.Create(rssURL);
   System.Net.WebResponse myResponse =myRequest.GetResponse();
   System.IO.Stream rssStream=myResponse.GetResponseStream();
   System.Xml.XmlDocument rssDoc=new System.Xml.XmlDocument();
   rssDoc.Load(rssStream);
   
   System.Xml.XmlNodeList rssItems=rssDoc.SelectNodes("rss/channel/item");
   string title="";
   string link="";
   string description="";

 
    System.Xml.XmlNode rssDetail;

   for (int i = 0; i < rssItems.Count; i++)
    {

    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="";
    }
    tmpRet+="<p><b><a href='" + link + "' target='new'>" + title + "</a></b><br/>";
    tmpRet+=description + "</p>";
   }  
   if(tmpRet!="" & bWrite )
   {
    Response.Write(tmpRet);
   }
   return tmpRet;
  
  }
   
  private void GoURL(object send, System.EventArgs args)
  {
   string rssUrl="";
   if(send.Equals(ShowRUL1))
   {
    rssUrl=txtURL1.Text;
    if(txtURL2.Text!="")
    {
     string tmpstr="<div > <font size=5><b>Site: " + rssUrl + "</b></font><Br />"+ProcessRSSItem(rssUrl,false)+"</div >";
     RSS1.Text=tmpstr;
    }
    else
    {
     RSS1.Text="Result1";
    }
   }
   if(send.Equals(ShowRUL2))
   {
    rssUrl=txtURL2.Text;
    if(txtURL2.Text!="")
    {
     string tmpstr="<div > <font size=5><b>Site: " + rssUrl + "</b></font><Br />"+ProcessRSSItem(rssUrl,false)+"</div >";
     RSS2.Text=tmpstr;
    }
    else
    {
     RSS2.Text="Result2";
    }
   }

  }
  
</script>
<form id="form1" runat="server">
    <table width="100%">
        <tbody>
            <tr>
                <td valign="top" width="15%">URL1 </td>
                <td valign="top" width="70%"><asp:textbox id="txtURL1" runat="server" width="98%">http://www.codeguru.com/icom_includes/feeds/codeguru/rss-all.xml</asp:textbox> </td>
                <td valign="top" width="10%"><asp:linkbutton id="ShowRUL1" onclick="GoURL" runat="server">GO</asp:linkbutton> </td>
            </tr>
            <tr>
                <td valign="top" width="15%">URL2 </td>
                <td valign="top" width="70%"><asp:textbox id="txtURL2" runat="server" width="98%">http://www.developer.com/icom_includes/feeds/special/dev-5.xml</asp:textbox> </td>
                <td valign="top" width="10%"><asp:linkbutton id="ShowRUL2" onclick="GoURL" runat="server">GO</asp:linkbutton> </td>
            </tr>
            <tr>
                <td width="100%" colspan="2">
                <table>
                    <tbody>
                        <tr>
                            <td><asp:label id="RSS1" runat="server">Result1</asp:label> </td>
                        </tr>
                        <tr>
                            <td>
                            <div><!--    Response.Write("<hr  />");
                            --></div>
                            </td>
                        </tr>
                        <tr>
                            <td><asp:label id="RSS2" runat="server">Result2</asp:label> </td>
                        </tr>
                    </tbody>
                </table>
                </td>
            </tr>
        </tbody>
    </table>
</form>

 

*/