Domanda

I want to use the RSS2 extensions feature to add my own non-standard elements to my RSS feed as described here:

http://cyber.law.harvard.edu/rss/rss.html#extendingRss:

However I don't think that the .Net Rss20FeedFormatter class supports this feature.

My code looks something like this:

public Rss20FeedFormatter GetRSS()
{
  var feed = new SyndicationFeed(....);
  feed.Items = new List<SyndicationItem>();
  // add items to feed
  return new Rss20FeedFormatter(feed);
}

If it doesn't support it is there any alternative to just creating the XML element by element?

È stato utile?

Soluzione

Here's my findings. Took me a while to figure it all out.

This is what you do, your feed has to have a namespace

XNamespace extxmlns = "http://www.yoursite.com/someurl";
feed.AttributeExtensions.Add(new XmlQualifiedName("ext", XNamespace.Xmlns.NamespaceName), extxmlns.NamespaceName);
feed.ElementExtensions.Add(new XElement(extxmlns + "link", new XAttribute("rel", "self"), new XAttribute("type", "application/rss+xml")));
return new Rss20FeedFormatter(feed, false);

Your items need to be a derived class, and you write the extended properties in WriteElementExtensions, making sure you prefix them with the namespace (you don't have to but this is what is required to make it valid RSS).

class TNSyndicationItem : SyndicationItem

protected override void WriteElementExtensions(XmlWriter writer, string version)
{
  writer.WriteElementString("ext:abstract", this.Abstract);
  writer.WriteElementString("ext:channel", this.Channel);
}

The extended properties are ignore if you look in an RSS reader such as firefox, you'll need to write code to read them as well.

The url http://www.yoursite.com/someurl doesn't have to exist, but you need it to define the namespace and make the RSS valid. Normally you'll just put a page there which says something about what the feed should look like.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top