读取RSS(支持目前所有RSS版本)

using  System;
using  System.Collections.Generic;
using  System.Net;
using  System.IO;
using  System.Xml;
using  System.Xml.XPath;

public   class  RssDoc
{
    XmlDocument doc;
    List
<Hashtable> list;

    
public RssDoc()
    

    }


    
public RssDoc(XmlDocument doc)
    
{
        
this.doc = doc;
        Load();
    }


    
/// <summary>
    
/// 获取网络资源
    
/// </summary>
    
/// <param name="url">url</param>
    
/// <param name="timeOut">timeout(单位秒)</param>
    
/// <param name="useProxy">是否用代理</param>

    public static XmlDocument GetDoc(string url, int timeOut, bool useProxy)
    
{
        XmlDocument _doc 
= new XmlDocument();
        
try
        
{
            WebRequest req 
= WebRequest.Create(url);
            
if (useProxy)
            
{
                WebProxy proxy 
= new WebProxy("http://63.149.98.16:80/"true);
                req.Proxy 
= proxy;
            }

            req.Timeout 
= timeOut * 1000;
            WebResponse res 
= req.GetResponse();
            Stream rssStream 
= res.GetResponseStream();
            _doc.Load(rssStream);
            rssStream.Dispose();
            res.Close();
        }

        
catch
        
{
            _doc 
= null;
        }

        
return _doc;
    }


    
private void Load()
    
{
        list 
= new List<Hashtable>();

        XmlNodeList nodes 
= doc.GetElementsByTagName("item");
        
if (nodes==null||nodes.Count == 0)
            nodes 
= doc.GetElementsByTagName("entry");
        
if (nodes == null || nodes.Count == 0)
            
return;

        Hashtable ht;
        XmlNodeList ns;
        
string name;
        
string date;
        
foreach (XmlNode node in nodes)
        
{
            ht 
= new Hashtable();
            ns 
= node.ChildNodes;
            
try
            
{
                
foreach (XmlNode n in ns)
                
{
                    name 
= n.Name.ToLower();
                    
if (name.Contains("link"))
                    
{
                        
if (n.Attributes["href"!= null)
                            ht[
"link"= n.Attributes["href"].Value.Trim();
                        
else ht["link"= n.InnerText.Trim();
                        
continue;
                    }

                    
if (name.Contains("title"))
                    
{
                        ht[
"title"= n.InnerText.Trim();
                        
continue;
                    }

                    
if (name.Contains("category"))
                    
{
                        
if (ht["category"== null) ht["category"= n.InnerText.Trim();
                        
else ht["category"= ht["category"].ToString() + "," + n.InnerText.Trim();
                        
continue;
                    }

                    
if (name.Contains("date"))
                    
{
                        date 
= n.InnerText;
                        
if (date != "")
                        
{
                            
if (date.Contains(",")) date = date.Substring(date.IndexOf(","+ 1);
                            date 
= date.Trim();
                            
if (date.Split(' ').Length > 4) date = date.Replace(date.Split(' ')[4], "");
                            
if (date.Contains(".")) date = date.Split('.')[0].Trim();
                            date 
= date.Replace("T"" ");
                            
if (date.Substring(date.LastIndexOf(":"+ 1).Length > 2)
                                date 
= date.Substring(0, date.LastIndexOf(":"+ 3);
                            
try { date = DateTime.Parse(date.Trim()).ToString(); }
                            
catch { date = DateTime.Now.ToString(); }
                        }

                        
else date = DateTime.Now.ToString();
                        ht[
"pubdate"= date;
                        
continue;
                    }

                    
if (name.Contains("description"))
                    
{
                        ht[
"description"= n.InnerText.Trim();
                        
continue;
                    }

                    
if (name.Contains("content"))
                    
{
                        ht[
"description"= n.InnerText.Trim();
                        
continue;
                    }

                    
if (name.Contains("summary"))
                    
{
                        
if (ht["description"== null) ht["description"= n.InnerText.Trim();
                    }

                }

            }

            
catch
            
{
                
continue;
            }

            
if (ht["link"== null) ht["link"= "";
            
if (ht["title"== null || ht["title"].ToString() == "") ht["title"= ht["link"].ToString();
            
if (ht["category"== null) ht["category"= "";
            
if (ht["pubdate"== null) ht["pubdate"= DateTime.Now.ToString();
            
if (ht["description"== null) ht["description"= "";

            list.Add(ht);
        }

    }


    
public List<Hashtable> Items
    
{
        
get
        
{
            
return list;
        }

    }


}

 

使用:

                    XmlDocument doc  =  RssDoc.GetDoc(url,  3 false );

                    RssDoc rssdoc 
=   new  RssDoc(doc);

                    
string  title  =   "" ;
                    
string  link  =   "" ;
                    
string  description  =   "" ;
                    
string  cate  =   "" ;
                    
string  time  =   "" ;

                    
foreach  (Hashtable hs  in  rssdoc.Items)
                    
{
                        description 
= hs["description"].ToString();

                        title 
= hs["title"].ToString();
                        link 
= hs["link"].ToString();

                        time 
= hs["pubdate"].ToString();
                        cate 
= hs["category"].ToString();
                    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值