using System.IO;
using System.Xml;
using System.Net;

 

 

  public int viewcount = 5;
    public int viewtitlecount = 10;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           RepOne.DataSource = viewInfo("http://www.youku.com/index/rss_cool_v/",viewcount,viewtitlecount);
           RepOne.DataBind();
           RepTwo.DataSource = viewInfo("http://news.baidu.com/n?cmd=1&class=housenews&tn=rss&sub=0", viewcount, viewtitlecount);
           RepTwo.DataBind();
           RepThree.DataSource = viewInfo("http://rss.sina.com.cn/news/marquee/ddt.xml", viewcount, viewtitlecount);
           RepThree.DataBind();
           RepFour.DataSource = viewInfo("http://rss.business.sohu.com/rss/caijingpinglun.xml", viewcount, viewtitlecount);
           RepFour.DataBind();
           RepFive.DataSource = viewInfo("http://www.cnblogs.com/nova_zhang/rss", viewcount, viewtitlecount);
           RepFive.DataBind();
           RepSix.DataSource = viewInfo("http://ent.163.com/special/00031K7Q/rss_toutiao.xml", viewcount, viewtitlecount);
           RepSix.DataBind();
           RepSeven.DataSource = viewInfo("http://bbs.linmiaoke.com/rss.php?auth=ba7dkXNDhwrD2J9npBuImRw4CWJkzjGRxHQsECan0T%2B7YeixcGAzhKW%2Fkg", viewcount, viewtitlecount);
           RepSeven.DataBind();
        }
    }
    // viewInfo
    private DataTable viewInfo(string url,int totalcount,int titlecount)
    {
        try
        {
            //使用rssURL的值建立了一个WebRequest对象
            WebRequest myRequest = WebRequest.Create(url);
            //这个请求的响应将会被放到一个WebResponse对象里
            WebResponse myResponse = myRequest.GetResponse();
            //然后这个WebResponse对象被用来建立一个流来取出XML的值
            Stream rssStream = myResponse.GetResponseStream();
            //然后可以使用一个XmlDocument对象来存储流中的XML内容。XmlDocument对象用来调入XML的内容:
            XmlDocument rssDoc = new XmlDocument();
            //因为RSS Feed不只是一个XML文件,我们可以假设里面包含了一些RSS标准的规定。这里,我们假设使用了RSS 2.0

            rssDoc.Load(rssStream);
            DataTable dt = new DataTable();
            dt.Columns.Add("title", typeof(string));
            dt.Columns.Add("link", typeof(string));
            dt.Columns.Add("pubDate", typeof(string));
            //dt.Columns.Add("description", typeof(string));
            //具体的来说,每个项应该在rss/channel/里。使用XPath表达,一个项节点列表可以如下方式创建
            XmlNodeList rssItems = rssDoc.SelectNodes("rss/channel/item");
            if (rssItems.Count < totalcount)
            {
                totalcount = rssItems.Count;
            }
            for (int i = 0; i < totalcount; i++)
            {
                DataRow row = dt.NewRow();
                //rssItems存储了从RSS里获得所有项节点的信息。这样就可取得内部所需要的信息了。这里,标题、链接和每个项的描述将会被显示。在rssItems中存储的每个项,每个标记(tag)元素都可以用SelectSingleNode方法提取出来。返回的值将被赋给一个XMLNode对象。以下代码获取了一个标题节点
                XmlNode rsstitle = rssItems.Item(i).SelectSingleNode("title");
                XmlNode rsslink = rssItems.Item(i).SelectSingleNode("link");
                XmlNode rsspubdate = rssItems.Item(i).SelectSingleNode("pubDate");
                //XmlNode rssdescription = rssItems.Item(i).SelectSingleNode("description");

                string title = rsstitle.InnerText;
                string link = rsslink.InnerText;
                string pubdate = rsspubdate.InnerText;
                //string description = rssdescription.InnerText;
                if (title.Length < 1)
                {
                    title = rsstitle.InnerXml;
                }
                if (link.Length < 1)
                {
                    link = rsslink.InnerXml;
                }
                if (pubdate.Length < 1)
                {
                    pubdate = rsspubdate.InnerXml;
                }
                //if (description.Length < 1)
                //{
                //    description = rssdescription.InnerXml;
                //}
                title = title.Replace("<![CDATA[", "");
                title = title.Replace("]]>", "");
                if (title.Length > titlecount)
                {
                    title = MySubstring.mysubstr(title, titlecount * 2) +"...";
                }
                link = link.Replace("<![CDATA[", "");
                link = link.Replace("]]>", "");
                pubdate = pubdate.Replace("<![CDATA[", "");
                pubdate = pubdate.Replace("]]>", "");
                pubdate = DateTime.Parse(pubdate).ToString("yyyy-MM-dd hh:mm:ss");
                //description = description.Replace("<![CDATA[", "");
                //description = description.Replace("]]>", "");
                row["title"] = "<a href='" + link + "' target='_blank'>" + title + "</a>";
                row["link"] = "<a href='" + link + "' target='_blank'>" + link + "</a>";
                row["pubDate"] = "<a href='" + link + "' target='_blank'>" + pubdate + "</a>";
                //row["description"] = "<a href='" + link + "' target='_blank'>" + description + "</a>";
                dt.Rows.Add(row);
            }
            return dt;
        }
        catch
        {
            return null;
        }
    }

 

 

 

/// <summary>
///MySubstring 的摘要说明
/// </summary>
public static class MySubstring
{
    public static string mysubstr(this string s, int length)
    {
        byte[] bytes = System.Text.Encoding.Unicode.GetBytes(s);
        int n = 0;  //  表示当前的字节数
        int i = 0;  //  要截取的字节数
        for (; i < bytes.GetLength(0) && n < length; i++)
        {
            //  偶数位置,如0、2、4等,为UCS2编码中两个字节的第一个字节
            if (i % 2 == 0)
            {
                n++;      //  在UCS2第一个字节时n加1
            }
            else
            {
                //  当UCS2编码的第二个字节大于0时,该UCS2字符为汉字,一个汉字算两个字节

                if (bytes[i] > 0)
                {
                    n++;
                }
            }
        }
        //  如果i为奇数时,处理成偶数
        if (i % 2 == 1)
        {
            //  该UCS2字符是汉字时,去掉这个截一半的汉字
            if (bytes[i] > 0)
                i = i - 1;
            //  该UCS2字符是字母或数字,则保留该字符
            else
                i = i + 1;
        }
        return System.Text.Encoding.Unicode.GetString(bytes, 0, i);
    }
}

/Files/nova_zhang/RSS2.0基本入门.pdf 

 

 

posted on 2010-05-13 16:30  Nova_Zhang  阅读(388)  评论(0编辑  收藏  举报