Rss20FeedFormatter Ignores TextSyndicationContent type for SyndicationItem.Summary

与世无争的帅哥 提交于 2019-12-03 06:55:10
Bart

Well @Page Brooks, I see this more as a solution then as a question :). Thanks!!! And to answer your question ( ;) ), yes, I definitely think this is a bug in the Rss20FeedFormatter (though I did not chase it as far), because had encountered precisely the same issue that you described.

You have a 'localhost:8732' referral in your post, but it wasn't available on my localhost ;). I think you meant to credit the 'PostProcessOutputBuffer' workaround to this post: http://damieng.com/blog/2010/04/26/creating-rss-feeds-in-asp-net-mvc

Or actually it is not in this post, but in a comment to it by David Whitney, which he later put in a seperate gist here: https://gist.github.com/davidwhitney/1027181

Thank you for providing the adaption of this workaround more to my needs, because I had found the workaround too, but was still struggling to do the adaptation from MVC. Now I only needed to tweak your solution to put the RSS feed to the current Http request in the .ashx handler that I was using it in.

Basically I'm guessing that the fix you mentioned using the CDataSyndicationContent, is from feb 2011, assuming you got it from this post (at least I did): SyndicationFeed: Content as CDATA?

This fix stopped working in some newer ASP.NET version or something, due to the code of the Rss20FeedFormatter changing to what you put in your post. This code change might as well have been an improvement for other stuff that IS in the MVC framework, but for those using the CDataSyndicationContent fix it definitely causes a bug!

I found the code for Cdata elsewhere

    public class CDataSyndicationContent : TextSyndicationContent
{
    public CDataSyndicationContent(TextSyndicationContent content)
        : base(content)
    {
    }

    protected override void WriteContentsTo(System.Xml.XmlWriter writer)
    {
        writer.WriteCData(Text);
    }
}

Code to call it something along the lines:

item.Content = new Helpers.CDataSyndicationContent(new TextSyndicationContent("<span>TEST2</span>", TextSyndicationContentKind.Html));

However the "WriteContentsTo" function wasn't being called.

Instead of Rss20FeedFormatter I tried Atom10FeedFormatter - and it worked! Obviously this gives Atom feed rather than traditional RSS - but worth mentioning.

Output code is:

//var formatter = new Rss20FeedFormatter(feed);
    Atom10FeedFormatter formatter = new Atom10FeedFormatter(feed);
    using (var writer = XmlWriter.Create(response.Output, new XmlWriterSettings { Indent = true }))
    {
        formatter.WriteTo(writer);
    }
string stylesheet = @"<xsl:stylesheet version=""1.0"" xmlns:xsl=""http://www.w3.org/1999/XSL/Transform""><xsl:output cdata-section-elements=""description"" method=""xml"" indent=""yes""/></xsl:stylesheet>";
XmlReader reader = XmlReader.Create(new StringReader(stylesheet));
XslCompiledTransform t = new XslCompiledTransform(true);
t.Load(reader);

using (MemoryStream ms = new MemoryStream())
{
    XmlWriter writer = XmlWriter.Create(ms, t.OutputSettings);
    rssFeed.WriteTo(writer);  // rssFeed is Rss20FeedFormatter 
    writer.Flush();
    ms.Position = 0;
    string niko = Encoding.UTF8.GetString(ms.ToArray());
}

I'm sure someone pointed this out already but this a stupid workaround I used. t.OutputSettings is of type XmlWriterSettings with cdataSections being populated with a single XmlQualifiedName "description".

Hope it helps someone else.

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