【发布时间】:2020-08-29 14:10:27
【问题描述】:
我正在寻找一种合适的方式来从以下显示的网站读取和显示 RSS 提要:-
http://feeds.feedburner.com/zerohedge/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>
提前致谢。
【问题讨论】: