繁体   English   中英

可视C#速成版中的Rss Reader

[英]Rss Reader in visual C# express edition

嗨,我正在尝试用可视C#express创建RSS阅读器。 加载表单时,我需要将rss feed读入文本框。 我以前从未使用过RSS feed,并且遇到的所有示例都是在Visual Studio中完成的,看来我不能使用它:

       (XmlReader reader = XmlReader.Create(Url))

到目前为止,这就是我所得到的。 没用

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        var s = RssReader.Read("http://ph.news.yahoo.com/rss/philippines");
        textBox1.Text = s.ToString();
    }
    public class RssNews
    {
        public string Title;
        public string PublicationDate;
        public string Description;
    }

    public class RssReader
    {
        public static List<RssNews> Read(string url)
        {
            var webResponse = WebRequest.Create(url).GetResponse();
            if (webResponse == null)
                return null;
            var ds = new DataSet();
            ds.ReadXml(webResponse.GetResponseStream());

            var news = (from row in ds.Tables["item"].AsEnumerable()
                        select new RssNews
                        {
                            Title = row.Field<string>("title"),
                            PublicationDate = row.Field<string>("pubDate"),
                            Description = row.Field<string>("description")
                        }).ToList();
            return news;
        }
    }

我不确定该怎么办。 请帮忙。

很好,您的代码可以按预期工作,因为您要返回RSSNews项列表,但是您以错误的方式将其分配给文本框。 textBox1.Text = s.ToString(); 结果将给出System.Collections.Generic.List....

您的方法是从数据集中读取RssNews项目,并针对Feed返回约23个项目。 您需要遍历这些项目并在文本框中显示其文本,如果可以使用GridView或类似控件显示这些结果,则更好。

您可以在Main方法中尝试以下代码:

        var s = RssReader.Read("http://ph.news.yahoo.com/rss/philippines");
        StringBuilder sb = new StringBuilder();
        foreach (RssNews rs in s)
        {
            sb.AppendLine(rs.Title);
            sb.AppendLine(rs.PublicationDate);
            sb.AppendLine(rs.Description);
        }

        textBox1.Text = sb.ToString();

这将为RssNews的每个项目创建一个字符串,并将结果显示在textBox1中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM