【问题标题】:Read RSS Feed from sites and Display HTML content from it using C#使用 C# 从站点读取 RSS 提要并从中显示 HTML 内容
【发布时间】:2020-08-29 14:10:27
【问题描述】:

我正在寻找一种合适的方式来从以下显示的网站读取和显示 RSS 提要:-

http://feeds.feedburner.com/zerohedge/feed

http://www.nirmaltv.com/feed/

以下是我用于从上述网站读取 RSS Feed 的代码。

  public virtual IList<NewsFeedItem> ParseRss(string url)
  {
    try
    {
        XDocument doc = XDocument.Load(url);

        var entries = from item in doc.Root.Descendants().First(i => i.Name.LocalName == "channel").Elements().Where(i => i.Name.LocalName == "item")
                      select new NewsFeedItem
                      {                                  
                          Content = item.Elements().First(i => i.Name.LocalName == "description").Value,
                          Link = item.Elements().First(i => i.Name.LocalName == "link").Value,
                          PublishDate = ParseDate(item.Elements().First(i => i.Name.LocalName == "pubDate").Value),
                          Title = item.Elements().First(i => i.Name.LocalName == "title").Value
                      };
        return entries.ToList();
    }
    catch (Exception ex)
    {
        return new List<NewsFeedItem>();
    }
}

private DateTime ParseDate(string date)
{
    DateTime result;
    if (DateTime.TryParse(date, out result))
        return result;
    else
        return DateTime.MinValue;
}

public class NewsFeedItem
{
 public string Link { get; set; }
 public string Title { get; set; }
 public string Content { get; set; }
 public DateTime PublishDate { get; set; }        

  public NewsFeedItem()
  {
    Link = string.Empty;
    Title = string.Empty;
    Content = string.Empty;
    PublishDate = DateTime.Today;            
  }
}

并且此代码用于 WPF 窗口的加载事件。此 WPF 应用程序有一个窗口,其中包含一个列表框。我正在尝试将 RSS 提要显示到 WPF 列表框中[或者请建议任何其他最佳控件来显示 RSS 提要]。

 private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        Application.Current.Dispatcher.Invoke((Action)delegate {
        Window parser = new Window ();
        var items = parser.ParseRss(Url);
        lstbox.ItemsSource = items;
        });
    }

当我使用 ParseRss 方法“解析这些网站”时,我得到了 HTML 格式的字符串(这意味着字符串以段落标记或 href 标记等开头)。

我想在列表框中按照 HTML 格式的字符串显示相应的网页或网页内容。 这怎么可能?

XAML 窗口是 ;

    <ListBox x:Name="lstbox" Height="300" Width="500" 
             ScrollViewer.VerticalScrollBarVisibility="Visible" Margin="3">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Border BorderBrush="Green" BorderThickness="1" Margin="3" CornerRadius="5" Height="260" Width="500">
                <StackPanel Orientation="Vertical">
                    <TextBlock Background="DarkGray" Foreground="AntiqueWhite" FontSize="16" FontWeight="Bold" Text="{Binding Title}" />
                    <TextBlock Margin="3" Text="{Binding Link}" />
                    <TextBlock Text="{Binding Content}" TextWrapping="Wrap" />
                    <TextBlock Text="{Binding PublishDate}" />
                </StackPanel>
            </Border>                   
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

提前致谢。

【问题讨论】:

    标签: c# html wpf web


    【解决方案1】:

    尝试以下 xml linq。我看起来描述和编码是一样的。见下面的代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    using System.Xml;
    using System.Xml.Linq;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            const string URL = "http://www.nirmaltv.com/feed/";
            static void Main(string[] args)
            {
    
                XDocument doc = XDocument.Load(URL);
                XElement channel = doc.Root;
                XNamespace ns = channel.GetDefaultNamespace();
                XNamespace nsContent = channel.GetNamespaceOfPrefix("content");
                XNamespace nsRDF = channel.GetNamespaceOfPrefix("rdf");
    
                List<Item> items = doc.Descendants(ns + "item").Select(x => new Item()
                {
                    title = (string)x.Element(ns + "title"),
                    link = (string)x.Element(ns + "link"),
                    comments = (string)x.Element(ns + "comments"),
                    pubDate = (DateTime)x.Element(ns + "pubDate"),
                    category = (string)x.Element(ns + "category"),
                    guid = (string)x.Element(ns + "guid"),
                    description = (string)x.Element(ns + "description"),
                    encoded = (string)x.Element(nsContent + "encoded"),
                    sequence = (string)x.Element(nsRDF + "Seq"),
                }).ToList();
            }
        }
        public class Item
        {
            public string title { get; set; }
            public string link { get; set; }
            public string comments { get; set; }
            public DateTime  pubDate { get; set; }
            public string category { get; set; }
            public string guid { get; set; }
            public string description { get; set; }
            public string encoded { get; set; }
            public string sequence { get; set; }
        }
    }
    

    【讨论】:

    • 注意:代码正在运行,刚刚开始给我一个 401 错误。
    • 您必须添加要放入列表框的代码。添加绑定源。
    • 是的,我已经完成了 UI 更改。谢谢你的帮助。但是一些 html 标签以 w3.org/1999/02/22-rdf-syntax-ns#" 开头并且无法正确解析它。遇到这种情况怎么办???
    • 此网址 - hartford.craigslist.org/search/… 开头
    • 我尝试了您的最新代码,但仍然无法正常工作。 List 项的末尾始终为 0。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多