【问题标题】:Read specific tag in a RSS feed阅读 RSS 提要中的特定标签
【发布时间】:2012-07-14 21:02:51
【问题描述】:

我有以下 RSS 提要。我想阅读特定描述标签内的信息。例如,当标题标签包含当前日期时,我想获取描述标签中的信息。我不知道该怎么做。请帮忙

<item>
<title>Forecast for Saturday as of Jul. 14 5:30 AM IST</title> //If today is Saturday get information in description tag
<link>http://www.wunderground.com/global/stations/43466.html</link>    
  <description>
Thunderstorm. Low:26 &amp;deg; C.
  </description>
  <pubDate>Sat, 14 Jul 2012 00:00:00 GMT</pubDate>
  <guid isPermaLink="false">1342267200-1-night</guid>
</item>
<item>
<title>Forecast for Sunday as of Jul. 14 5:30 AM IST</title>
<link>http://www.wunderground.com/global/stations/43466.html</link>
  <description>
Chance of a Thunderstorm. High:30 &amp;deg; C.
  </description>
  <pubDate>Sat, 14 Jul 2012 00:00:00 GMT</pubDate>
  <guid isPermaLink="false">1342353600-2-day</guid>
 </item>

我能够通过以下方式获取当前日期:

string datenow = DateTime.Today.ToString("dddd / M / yyyy");
string[] words= datenow.Split(' ');
string day = words[0];

这就是我阅读 RSS 提要的方式:

 public class RssReader
    {
        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();
        }
    }

【问题讨论】:

    标签: c# rss rss-reader


    【解决方案1】:

    根据您的要求,您只想获取当天的描述。您有带有标题,描述..等的新闻提要列表现在您可以更改您的课程(在您的课程中添加一个方法)如果当天是当天

    公共静态字符串 GetCurrentDayDescription(){

       var lst = Read("url");
       var resDescription = (from x in lst where x.Title.Contains(day) 
                              select x.Description).ToArray() ;
        return resDescription[0] ;
    }
    

    【讨论】:

      【解决方案2】:

      您应该能够使用 XmlSerializer 直接反序列化 rss 提要,无需手动映射。您需要修改 RssNews 对象以正确映射,例如:

      [XmlRoot(ElementName="item")]
      public class RssNews
      {
          [XmlElement(ElementName = "title")]
          public string Title { get; set; }
      
          [XmlElement(ElementName = "pubDate")]
          public string PublicationDate  { get; set; }
      
          [XmlElement(ElementName = "description")]
          public string Description { get; set; }
      
          [XmlElement(ElementName = "link")]
          public string Link { get; set; }
      
          [XmlElement(ElementName = "guid")]
          public string Description { get; set; }
      }
      

      现在你应该可以使用反序列化器了:

          var feed = new List<RssNews>();
          using (var webClient = new WebClient())
          {
      
              string result = webClient.DownloadString(url);
              using (var stringReader = new StringReader(result))
              {
                  var serializer = new XmlSerializer(feed.GetType());
                  feed = (List<RssNews>)serializer.Deserialize(stringReader);
              }
          }
          return feed;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-10-04
        • 2012-07-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-12
        • 1970-01-01
        相关资源
        最近更新 更多