0
我试图让这个如何消耗RSS提要,并读取DC:
我使用this code工作creator元素,它本身工作正常,但现在我试图让下面的元素被读为:dc:creator并且不能。
类文件:
public class RSSItem
{
XNamespace dc="http://purl.org/dc/elements/1.1/";
public string Title { get; set; }
public string Description { get; set; }
public string PubDate { get; set; }
public string Link { get; set; }
public string strGuid { get; set; }
public string Author { get; set; }
public string Dc_creator { get; set; }
// Next we’ll modify the constructor. First add a parameter list to the constructor:
public RSSItem(string title, string description, string link, string guid,
string pubDate, string (dc:creator) //<-- not working but what to use instead)
{
// This constructor will be used to parse the RSS xml. Add the following code to the constructor:
Title = title;
Description = description;
Link = link;
strGuid = guid;
PubDate = pubDate;
Dc_creator = dc:creator; <-- not working but what to use instead
显然,这并不工作:
string (dc:creator)) in the constructor
page.cshtml:
<div>
@{
XNamespace dc = XNamespace.Get("http://purl.org/dc/elements/1.1/");
XDocument rss = XDocument.Load("http://rss.xml");
var items = from elem in rss.Elements("rss").Elements("channel").Elements("item")
select elem;
foreach (var item in items)
{
RSSItem rssItem = new RSSItem(
item.Element("title").Value,
item.Element("link").Value,
item.Element("guid").Value,
item.Element("description").Value,
//item.Element("author").Value,
item.Element("pubDate").Value,
item.Element(dc + "creator").Value
);
<span class="rssStyle">
<div>
<b>@rssItem.Title</b>
</div>
etc etc
那么,如何管理有类的构造函数读取dc:creator
元素?
好的,谢谢!我猜这是处理显示部分,但我的问题更多的是关于类内部的构造函数以及如何传递dc:creator thru,因为它不会将冒号传递给变量名称。 在代码中表示此部分: //接下来我们将修改构造函数。首先参数列表添加到构造: 公众的RSSItem(字符串标题,描述字符串,字符串link,字符串GUID, 串pubdate的,串(DC:创建者)// < - 不工作,但改用什么) – lars
你可以将'creator'作为普通字符串传递,并且在构造函数中可以简单地执行'Dc_creator = string.Format(“dc:{0}”,creator);'告诉我这是否可行。 –
简直太棒了!是的!那样做了! 你是什么亲! 谢谢!现在我又学到了新的东西! – lars