C# visualc速成版中的Rss阅读器

C# visualc速成版中的Rss阅读器,c#,rss,rss-reader,C#,Rss,Rss Reader,您好,我正在尝试在visual C express中创建RSS阅读器。当表单加载时,我需要将rss提要读入文本框。我以前从未使用过RSS提要,我遇到的所有示例都是在visual studio中完成的,似乎我无法使用以下内容: (XmlReader reader = XmlReader.Create(Url)) 这就是我目前所得到的。它不起作用 using System; using System.Collections.Generic; using System.Componen

您好,我正在尝试在visual C express中创建RSS阅读器。当表单加载时,我需要将rss提要读入文本框。我以前从未使用过RSS提要,我遇到的所有示例都是在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项,并根据提要返回大约23个项。您需要遍历这些项目并在文本框中显示其文本,如果您可以使用GridView或类似控件来显示这些结果,则更好

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


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

您想在文本框中显示什么?返回了XML还是别的什么?是的,我想显示RSS的文本feed@Habib哪一个是这里的主要方法?@envyM6,这是老方法,但我想主要方法应该是Page_Load事件,OP试图将结果分配给TextBox。我知道它的老方法,但我试图理解这一点,并且努力。。。因此,EventArgs e{var s=RssReader.R代替私有void Form1_Loadobject sendereadhttp://ph.news.yahoo.com/rss/philippines;textBox1.Text=s.ToString;}它将是…@envyM6,yes用此代码替换Form1_Load事件中的代码。private void Form1_Loadobject发送方,EventArgs e{var s=RssReader.Readhttp://ph.news.yahoo.com/rss/philippines;StringBuilder sb=新建StringBuilder;foreach RssNews rs{sb.AppendLiners.Title;sb.AppendLiners.PublicationDate;sb.AppendLiners.Description;}textBox1.Text=sb.ToString;}
        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();