6

RSS フィードを読み取って C# アプリケーションに表示しようとしています。以下のコードを使用しましたが、他の RSS フィードでも完全に機能します。この RSS フィード ---> http://ptwc.weather.gov/ptwc/feeds/ptwc_rss_indian.xmlを読みたいのですが、以下のコードが機能しません。エラーは発生しませんが、何も起こりません。RSS フィードを表示するテキスト ボックスが空です。助けてください。私は何を間違っていますか?

    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;
        }
    }


    private string covertRss(string url) 
    {
        var s = RssReader.Read(url);
        StringBuilder sb = new StringBuilder();
        foreach (RssNews rs in s)
        {
            sb.AppendLine(rs.Title);
            sb.AppendLine(rs.PublicationDate);
            sb.AppendLine(rs.Description);
        }

        return sb.ToString();
    }

//フォームロードコード///

 string readableRss;
 readableRss = covertRss("http://ptwc.weather.gov/ptwc/feeds/ptwc_rss_indian.xml");
            textBox5.Text = readableRss;
4

1 に答える 1

9

項目にカテゴリが 2 回指定されているため、DataSet.ReadXml メソッドが失敗したようですが、名前空間が異なります。

これはうまくいくようです:

public static List<RssNews> Read(string url)
{
    var webClient = new WebClient();

    string result = webClient.DownloadString(url);

    XDocument document = XDocument.Parse(result);

    return (from descendant in document.Descendants("item")
            select new RssNews()
                {
                    Description = descendant.Element("description").Value,
                    Title = descendant.Element("title").Value,
                    PublicationDate = descendant.Element("pubDate").Value
                }).ToList();
}
于 2012-07-06T12:07:50.697 に答える