0

私のアプリケーションでは、動的なrssフィードのURLをユーザーが保存します。だから私はRSSフィードによって返されるそのxmlをどのように読むことができるか知りたいです。そのxmlの構造はどうなりますか?titleいくつかのフィードURLを確認しましたが、それらのほとんどにタグが付いていることに気付きましたがdescription、これについてはよくわかりません。これらの2つのタグを取得した場合、xmlを解析しますが、常に使用できるとは限らない場合、その場合はどのようにxmlを解析できますか。

これらの2つには、タイトルと説明のタグが含まれています

http://rss.news.yahoo.com/rss/entertainment
http://xml.weather.yahoo.com/forecastrss?p=USCA1116
4

1 に答える 1

0

最初にXMLファイルを読み取る必要があります。XPathまたはLinqtoXMLを使用することをお勧めします。すでに述べたように、フィードを構成する3つの主要な要素があります。「タイトル」、「リンク」、「説明」。

少し前に私はそれを行うためのコードを書きました、これがあなたのために働くことを願っています。

この2つのエンティティを作成しました。

    public class RssFeed
    {
        public string Title { get; set; }

        public string Link { get; set; }

        public string Description { get; set; }

        public string PubDate { get; set; }

        public string Language { get; set; }

        public ObservableCollection<RssItem> RssItems { get; set; }
    }

    public class RssItem
    {
        public string Title { get; set; }

        public string Description { get; set; }

        public string Link { get; set; }
    }

次に、このメソッドで、LinqtoXMLを使用してXMLファイルからすべての要素を読み取ります

private static void ReadFeeds()
        {
            string uri = @"http://news.yahoo.com/rss/entertainment";

            WebClient client = new WebClient();
            client.DownloadStringAsync(new Uri(uri, UriKind.Absolute));

            client.DownloadStringCompleted += (s, a) =>
            {
                if (a.Error == null && !a.Cancelled)
                {
                    var rssReader = XDocument.Parse(a.Result);

                    var feed = (from rssFeed in rssReader.Descendants("channel")
                                select new RssFeed()
                                {
                                    Title = null != rssFeed.Descendants("title").FirstOrDefault() ?
                                            rssFeed.Descendants("title").First().Value : string.Empty,

                                    Link = null != rssFeed.Descendants("link").FirstOrDefault() ?
                                            rssFeed.Descendants("link").First().Value : string.Empty,

                                    Description = null != rssFeed.Descendants("description").FirstOrDefault() ?
                                            rssFeed.Descendants("description").First().Value : string.Empty,

                                    PubDate = null != rssFeed.Descendants("pubDate").FirstOrDefault() ?
                                            rssFeed.Descendants("pubDate").First().Value : string.Empty,

                                    Language = null != rssFeed.Descendants("language").FirstOrDefault() ?
                                            rssFeed.Descendants("language").First().Value : string.Empty
                                }).Single();

                    var rssFeeds = (from rssItems in rssReader.Descendants("item")
                                    select new RssItem()
                                    {
                                        Title = null != rssItems.Descendants("title").FirstOrDefault() ?
                                                 rssItems.Descendants("title").First().Value : string.Empty,

                                        Link = null != rssItems.Descendants("link").FirstOrDefault() ?
                                                 rssItems.Descendants("link").First().Value : string.Empty,

                                        Description = null != rssItems.Descendants("description").FirstOrDefault() ?
                                                 rssItems.Descendants("description").First().Value : string.Empty,
                                    }).ToList();

                    feed.RssItems = new ObservableCollection<RssItem>(rssFeeds);
                }
            };
        }

そして最後に、フィードを好きな場所に表示できます。

于 2012-11-07T17:32:20.830 に答える