12

I am using System.ServiceModel.Syndication.SyndicationFeed to create an rss feed from which I get this:

<rss xmlns:a10="http://www.w3.org/2005/Atom" version="2.0"><channel>...</channel></rss>

It is all working swimmingly, except for when I validate my feed.

The validator complains about the a10 namespace prefix and suggests that I use atom instead. That sounds reasonable.. except I can't see a straightforward way of changing the prefix.

Any ideas on ways of changing the prefix?

Derek Ekins
  • 11,215
  • 6
  • 61
  • 71
  • That looks like you have actually output your feed as RSS 2.0, not Atom 1.0 – see the different XML output examples at http://msdn.microsoft.com/en-us/library/system.servicemodel.syndication.syndicationfeed.aspx#code-snippet-3 – CBroe Mar 28 '13 at 10:11
  • 1
    Yeah it is rss, that is what I am expecting. For whatever reason the feed validator suggests having a link back to self using the atom namespace so I am just trying to adhere to that. Am adding a link like this - but then the validator wants a10 to be atom. – Derek Ekins Mar 28 '13 at 10:20
  • The validator's warning is just a suggestion. There's no reason you *shouldn't* change the prefix, but no valid parser cares about the difference. – Joe Mar 28 '13 at 12:01
  • 1
    Yeah I know it is just a suggestion, but I would like to change it anyway - if only for finding out how to do it, seems ridiculously difficult right now! – Derek Ekins Mar 28 '13 at 12:24

1 Answers1

24

To specify a custom name for the atom extensions you need to disable SerializeExtensionsAsAtom on the feed formatter:

var formatter = feed.GetRss20Formatter();
formatter.SerializeExtensionsAsAtom = false;

Then you need to add the namespace

XNamespace atom = "http://www.w3.org/2005/Atom";

feed.AttributeExtensions.Add(new XmlQualifiedName("atom", XNamespace.Xmlns.NamespaceName), atom.NamespaceName);

And now you can start using the extensions

feed.ElementExtensions.Add(new XElement(atom + "link", new XAttribute("href", feedLink), new XAttribute("rel", "self"), new XAttribute("type", "application/rss+xml")));

Finally write the feed to the response stream:

formatter.WriteTo(new XmlTextWriter(Response.Output));
Derek Ekins
  • 11,215
  • 6
  • 61
  • 71