Rss20FeedFormatter忽略SyndicationItem.Summary的TextSyndicationContent类型 [英] Rss20FeedFormatter Ignores TextSyndicationContent type for SyndicationItem.Summary

查看:120
本文介绍了Rss20FeedFormatter忽略SyndicationItem.Summary的TextSyndicationContent类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WCF项目中使用Rss20FeedFormatter类时,我试图用<![CDATA[ ]]>节包装我的描述元素的内容.我发现无论我做什么,描述元素的HTML内容总是经过编码的,而CDATA部分却从未添加.浏览Rss20FeedFormatter的源代码后,我发现在构建Summary节点时,它基本上会创建一个新的TextSyndicationContent实例,该实例将清除先前指定的任何设置(我认为).

While using the Rss20FeedFormatter class in a WCF project, I was trying to wrap the content of my description elements with a <![CDATA[ ]]> section. I found that no matter what I did, the HTML content of the description elements was always encoded and the CDATA section was never added. After peering into the source code of Rss20FeedFormatter, I found that when building the Summary node, it basically creates a new TextSyndicationContent instance which wipes out whatever settings were previously specified (I think).

我的代码

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

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

...(以下代码应在摘要"中包含CDATA部分)

SyndicationItem item = new SyndicationItem();
item.Title = new TextSyndicationContent(name);
item.Summary = new CDataSyndicationContent(
                   new TextSyndicationContent(
                         "<div>This is a test</div>", 
                         TextSyndicationContentKind.Html));

Rss20FeedFormatter代码 (AFAIK,由于这种逻辑,以上代码无法正常工作)

...
else if (reader.IsStartElement("description", ""))
   result.Summary = new TextSyndicationContent(reader.ReadElementString());
...

作为一种解决方法,我求助于使用RSS20FeedFormatter构建RSS,然后手动修补RSS.例如:

As a workaround, I've resorted to using the RSS20FeedFormatter to build the RSS, and then patch the RSS manually. For example:

        StringBuilder buffer = new StringBuilder();
        XmlTextWriter writer = new XmlTextWriter(new StringWriter(buffer));
        feedFormatter.WriteTo(writer ); // feedFormatter = RSS20FeedFormatter

        PostProcessOutputBuffer(buffer);
        WebOperationContext.Current.OutgoingResponse.ContentType = 
                                   "application/xml; charset=utf-8";
        return new MemoryStream(Encoding.UTF8.GetBytes(buffer.ToString()));      

...

    public void PostProcessOutputBuffer(StringBuilder buffer)
    {
        var xmlDoc = XDocument.Parse(buffer.ToString());
        foreach (var element in xmlDoc.Descendants("channel").First()
                                      .Descendants("item")
                                      .Descendants("description"))
        {
            VerifyCdataHtmlEncoding(buffer, element);
        }

        foreach (var element in xmlDoc.Descendants("channel").First()
                                      .Descendants("description"))
        {
            VerifyCdataHtmlEncoding(buffer, element);
        }

        buffer.Replace(" xmlns:a10=\"http://www.w3.org/2005/Atom\"",
                       " xmlns:atom=\"http://www.w3.org/2005/Atom\"");
        buffer.Replace("a10:", "atom:");
    }

    private static void VerifyCdataHtmlEncoding(StringBuilder buffer,
                                                XElement element)
    {
        if (!element.Value.Contains("<") || !element.Value.Contains(">"))
        {
            return;
        }

        var cdataValue = string.Format("<{0}><![CDATA[{1}]]></{2}>",
                                       element.Name,
                                       element.Value, 
                                       element.Name);
        buffer.Replace(element.ToString(), cdataValue);
    }

此替代方法的想法来自以下位置,我将其调整为与WCF而不是MVC一起使用. http://localhost:8732/Design_Time_Addresses/SyndicationServiceLibrary1/Feed1/

The idea for this workaround came from the following location, I just adapted it to work with WCF instead of MVC. http://localhost:8732/Design_Time_Addresses/SyndicationServiceLibrary1/Feed1/

我只是想知道这是否只是Rss20FeedFormatter中的错误,还是设计使然?另外,如果有人有更好的解决方案,我很想听听!

I'm just wondering if this is simply a bug in Rss20FeedFormatter or is it by design? Also, if anyone has a better solution, I'd love to hear it!

推荐答案

@Page Brooks,我认为这更多是作为解决方案,然后是一个问题:).谢谢!!!并回答您的问题(;)),是的,我绝对认为这是Rss20FeedFormatter中的一个错误(尽管我没有深入探讨),因为遇到了与您所描述的完全相同的问题.

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.

您的帖子中有一个'localhost:8732'引用,但在我的localhost上不可用;).我认为您打算将"PostProcessOutputBuffer"解决方法归功于此文章: http://damieng.com/blog/2010/04/26/creating-rss-feeds-in-asp-net-mvc

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

或者实际上它不是在这篇文章中,而是在大卫·惠特尼(David Whitney)对它的评论中,他后来在这里发表了要点: https://gist.github.com/davidwhitney/1027181

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

感谢您为我提供了这种变通方法的更多适应方法,因为我也找到了变通方法,但仍在努力从MVC进行变通.现在,我只需要调整您的解决方案即可将RSS提要放到我正在使用它的.ashx处理程序中的当前Http请求中.

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.

基本上,我猜您使用CDataSyndicationContent提到的修复程序来自2011年2月,假设您从这篇文章中得到了修复(至少我是这样做的): SyndicationFeed:内容是否为CDATA?

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?

由于Rss20FeedFormatter的代码更改为您发布的内容,因此此修复程序停止在某些较新的ASP.NET版本或某些版本中工作.此代码更改可能也对MVC框架中IS的其他内容进行了改进,但对于使用CDataSyndicationContent修复程序的人来说,肯定会导致错误!

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!

这篇关于Rss20FeedFormatter忽略SyndicationItem.Summary的TextSyndicationContent类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆