Rss20FeedFormatter忽略SyndicationItem.Summary的TextSyndicationContent类型

Pag*_*age 8 rss wcf syndication-feed

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

我的守则

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

    protected override void WriteContentsTo(System.Xml.XmlWriter writer)
    {
        writer.WriteCData(Text);
    }
}
Run Code Online (Sandbox Code Playgroud)

...(以下代码应使用CDATA部分包装摘要)

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

Rss20FeedFormatter代码 (AFAIK,上面的代码因为这个逻辑不起作用)

...
else if (reader.IsStartElement("description", ""))
   result.Summary = new TextSyndicationContent(reader.ReadElementString());
...
Run Code Online (Sandbox Code Playgroud)

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

        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()));      
Run Code Online (Sandbox Code Playgroud)

...

    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);
    }
Run Code Online (Sandbox Code Playgroud)

这个解决方法的想法来自以下位置,我只是将其调整为使用WCF而不是MVC.HTTP://本地主机:8732/Design_Time_Addresses/SyndicationServiceLibrary1/FEED1 /

我只是想知道这只是Rss20FeedFormatter中的一个错误还是设计?此外,如果有人有更好的解决方案,我很乐意听到它!

Bar*_*art 3

好吧@Page Brooks,我认为这更多地是一个解决方案,而不是一个问题:)。谢谢!!!并回答你的问题(;)),是的,我绝对认为这是 Rss20FeedFormatter 中的一个错误(尽管我没有追究它那么远),因为遇到了与你描述的完全相同的问题。

您的帖子中有“localhost:8732”推荐,但它在我的本地主机上不可用;)。我认为您的意思是将“PostProcessOutputBuffer”解决方法归功于这篇文章: http://damieng.com/blog/2010/04/26/creating-rss-feeds-in-asp-net-mvc

或者实际上它不在这篇文章中,而是在 David Whitney 的评论中,他后来在这里单独提出了要点: https: //gist.github.com/davidwhitney/1027181

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

基本上我猜测您提到的使用 CDataSyndicateContent 的修复是从 2011 年 2 月开始的,假设您从这篇文章中得到了它(至少我做到了): SyndicateFeed:内容为 CDATA?

由于 Rss20FeedFormatter 的代码更改为您在帖子中放置的内容,此修复程序在某些较新的 ASP.NET 版本或其他版本中停止工作。此代码更改也可能是对 MVC 框架中其他内容的改进,但对于那些使用 CDataSyndicateContent 修复的人来说,它肯定会导致错误!