简体   繁体   English

如何使用RSS提要并阅读dc:creator元素

[英]How to consume a RSS feed and read the dc:creator element

I am trying to get this to work 我正在努力使它起作用

I am using this code and it works fine by itself, but now I am trying to get the following element to be read: dc:creator and are not able to. 我正在使用此代码 ,它本身可以正常工作,但是现在我试图读取以下元素:dc:creator并且无法读取。

class file: 类文件:

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

Obviously this does not work: 显然这不起作用:

string (dc:creator)) in the constructor

page.cshtml: 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

So how do I manage to have the class constructor read the dc:creator element? 那么我如何设法让类构造函数读取dc:creator元素?

Not the exact solution, but I had done this using XmlDocument . 不是确切的解决方案,但是我使用XmlDocument做到了。 You will have to modify this according to your RssItem , I had similar object 您将不得不根据您的RssItem修改它,我有类似的对象

XmlDocument doc = new XmlDocument();
doc.LoadXml(rss);
//Get Channel Node
XmlNode channelNode = doc.SelectSingleNode("rss/channel");
if (channelNode != null) {
    //Add NameSpace
    XmlNamespaceManager nameSpace = new XmlNamespaceManager(doc.NameTable);
    nameSpace.AddNamespace("content", "http://purl.org/rss/1.0/modules/content/");
    nameSpace.AddNamespace("slash", "http://purl.org/rss/1.0/modules/slash/");
    nameSpace.AddNamespace("dc", "http://purl.org/dc/elements/1.1/");

    //Parse each item
    foreach (XmlNode itemNode in channelNode.SelectNodes("item")) {
        RssFeed rssItem = new RssFeed();
        rssItem.Guid = itemNode.SelectSingleNode("guid").InnerText;
        rssItem.Title = itemNode.SelectSingleNode("title").InnerText;
        rssItem.CreatedBy = itemNode.SelectSingleNode("dc:creator", nameSpace).InnerText;
        rssItem.Url = itemNode.SelectSingleNode("link").InnerText;
        rssItem.PubDate = DateTime.Parse(itemNode.SelectSingleNode("pubDate").InnerText);
        rssItem.CommentCount = itemNode.SelectSingleNode("slash:comments", nameSpace).InnerText;
        rssItem.Description = itemNode.SelectSingleNode("content:encoded", nameSpace).InnerText;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM