• 捕获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();
        }

  • 相关阅读:
    fiddler无法抓取360浏览器包的问题
    腾讯云服务器上发送邮件连接超时(无法发送)的相关问题
    jQuery获取后台动态添加上去的选择器
    NOIP 模拟 $84; m 宝藏$
    NOIP 模拟 $83; m 铺设道路$
    NOIP 模拟 $83; m 传统艺能$
    NOIP 模拟 $83; m 时代的眼泪$
    NOIP 模拟 $83; m 树上的数$
    NOIP 模拟 $80; m 百鸽笼$
    NOIP 模拟 $80; m 滑稽树下你和我$
  • 原文地址:https://www.cnblogs.com/hqbird/p/1318276.html
Copyright © 2020-2023  润新知