问题
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.
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:
<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?
回答1:
Not the exact solution, but I had done this using XmlDocument
. You will have to modify this according to your RssItem
, I had similar object
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;
}
}
来源:https://stackoverflow.com/questions/8925179/how-to-consume-a-rss-feed-and-read-the-dccreator-element