SyndicationFeed change namespace prefix from a10 to atom

前端 未结 1 1119
北海茫月
北海茫月 2021-02-20 05:47

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



        
相关标签:
1条回答
  • 2021-02-20 06:45

    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));
    
    0 讨论(0)
提交回复
热议问题