SyndicationFeed change namespace prefix from a10 to atom

谁说胖子不能爱 提交于 2019-12-10 02:47:41

问题


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?


回答1:


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));


来源:https://stackoverflow.com/questions/15678635/syndicationfeed-change-namespace-prefix-from-a10-to-atom

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!