捕获RSS新闻,远程获取

 public static string GetRssNews(string url,int count)
    {
        StringBuilder sb = new StringBuilder();
        WebRequest myRequest = WebRequest.Create(url);
        WebResponse myResponse = myRequest.GetResponse();

        Stream rssStream = myResponse.GetResponseStream();
        XmlDocument rssDoc = new System.Xml.XmlDocument();
        rssDoc.Load(rssStream);

        XmlNodeList rssItems = rssDoc.SelectNodes("rss/channel/item");

        string title = "";
        string link = "";
        string pubDate = "";
        sb.Append("<AllNews>");
        if (rssItems.Count > 0)
        {
            int num = 0;
            if (rssItems.Count >= count)
            {
                num = count;
            }
            else
            {
                num = rssItems.Count;
            }
            sb.Append("<newsCount>" + num.ToString() + "</newsCount>");
            for (int i = 0; i < num; i++)
            {
                XmlNode rssDetail = rssItems.Item(i).SelectSingleNode("title");
                if (rssDetail != null)
                {
                    title =InsApp.word.CreateCode.CutStr(rssDetail.InnerText.ToString(),44);
                }
                else
                {
                    title = "【点击查看新闻】";
                }

                rssDetail = rssItems.Item(i).SelectSingleNode("link");
                if (rssDetail != null)
                {
                    link = rssDetail.InnerText;
                }
                else
                {
                    link = "";
                }

                rssDetail = rssItems.Item(i).SelectSingleNode("pubDate");
                if (rssDetail != null)
                {
                    pubDate = rssDetail.InnerText;
                }
                else
                {
                    pubDate = "";
                }
                sb.Append("<News>");
                sb.Append("<title>");
                sb.Append(title);
                sb.Append("</title>");
                sb.Append("<link>");
                sb.Append(link);
                sb.Append("</link>");
                sb.Append("<pubDate>");
                sb.Append(pubDate);
                sb.Append("</pubDate>");
                sb.Append("</News>");
            }
        }
        else
        {
            sb.Append("<newsCount>0</newsCount>");
        }
        sb.Append("</AllNews>");

        return sb.ToString();
    }

posted @ 2008-10-23 22:27  Jason.Bird  阅读(202)  评论(0编辑  收藏  举报