C# 从站点读取RSS提要并使用C显示其中的HTML内容#
我正在寻找一种合适的方式来阅读和显示来自以下网站的RSS提要:- 下面是我用来阅读上述网站的RSS提要的代码C# 从站点读取RSS提要并使用C显示其中的HTML内容#,c#,html,wpf,web,C#,Html,Wpf,Web,我正在寻找一种合适的方式来阅读和显示来自以下网站的RSS提要:- 下面是我用来阅读上述网站的RSS提要的代码 public virtual IList<NewsFeedItem> ParseRss(string url) { try { XDocument doc = XDocument.Load(url); var entries = from item in doc.Root.Descendants().First(i
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;
}
}
当我使用ParseRss方法“解析这些网站”时,我得到的是HTML格式的字符串(这意味着该字符串以段落标记或a href标记等开头)
我想根据列表框中的HTML格式字符串显示相应的网页或web内容。
这怎么可能
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>
提前感谢。尝试以下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; }
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用系统数据;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序1
{
班级计划
{
常量字符串URL=”http://www.nirmaltv.com/feed/";
静态void Main(字符串[]参数)
{
XDocument doc=XDocument.Load(URL);
XElement channel=doc.Root;
XNamespace ns=channel.GetDefaultNamespace();
XNamespace nsContent=channel.GetNamespaceOfPrefix(“内容”);
XNamespace nsRDF=channel.GetNamespaceOfPrefix(“rdf”);
列表项=文档子体(ns+“项”)。选择(x=>newitem()
{
title=(字符串)x.Element(ns+“title”),
link=(字符串)x.Element(ns+“link”),
注释=(字符串)x.Element(ns+“注释”),
pubDate=(DateTime)x.Element(ns+“pubDate”),
category=(字符串)x.Element(ns+“category”),
guid=(字符串)x.Element(ns+“guid”),
description=(字符串)x.Element(ns+“description”),
encoded=(字符串)x.Element(nsContent+“encoded”),
序列=(字符串)x.Element(nsRDF+“Seq”),
}).ToList();
}
}
公共类项目
{
公共字符串标题{get;set;}
公共字符串链接{get;set;}
公共字符串注释{get;set;}
public DateTime pubDate{get;set;}
公共字符串类别{get;set;}
公共字符串guid{get;set;}
公共字符串说明{get;set;}
公共字符串编码{get;set;}
公共字符串序列{get;set;}
}
}
尝试以下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; }
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用系统数据;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序1
{
班级计划
{
常量字符串URL=”http://www.nirmaltv.com/feed/";
静态void Main(字符串[]参数)
{
XDocument doc=XDocument.Load(URL);
XElement channel=doc.Root;
XNamespace ns=channel.GetDefaultNamespace();
XNamespace nsContent=channel.GetNamespaceOfPrefix(“内容”);
XNamespace nsRDF=channel.GetNamespaceOfPrefix(“rdf”);
列表项=文档子体(ns+“项”)。选择(x=>newitem()
{
title=(字符串)x.Element(ns+“title”),
link=(字符串)x.Element(ns+“link”),
注释=(字符串)x.Element(ns+“注释”),
pubDate=(DateTime)x.Element(ns+“pubDate”),
category=(字符串)x.Element(ns+“category”),
guid=(字符串)x.Element(ns+“guid”),
description=(字符串)x.Element(ns+“description”),
encoded=(字符串)x.Element(nsContent+“encoded”),
序列=(字符串)x.Element(nsRDF+“Seq”),
}).ToList();
}
}
公共类项目
{
公共字符串标题{get;set;}
公共字符串链接{get;set;}
公共字符串注释{get;set;}
public DateTime pubDate{get;set;}
公共字符串类别{get;set;}
公共字符串guid{get;set;}
公共字符串说明{get;set;}
公共字符串编码{get;set;}
公共字符串序列{get;set;}
}
}
注意:代码正在运行,刚开始给我一个401错误。您必须添加代码以放入列表框。添加绑定源。是的,我已完成UI更改。谢谢你的帮助。但是一些html标签是以这个url开始的——以我用你的最新代码尝试过的url开始的,但是仍然不起作用。列表项的末尾始终为0。注意:代码正在运行,刚开始出现401错误。您必须添加要放入列表框的代码。添加绑定源。是的,我已完成UI更改。谢谢你的帮助。但是一些html标签是以这个url开始的——以我用你的最新代码尝试过的url开始的,但是仍然不起作用。列表项的末尾始终为0。