wcf - Rss20FeedFormatter 忽略 SyndicateItem.Summary 的 TextSyndicateContent 类型

标签 wcf rss syndication-feed

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

我的代码

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 代码 (据我所知,由于这个逻辑,上面的代码不起作用)

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

作为一种解决方法,我使用 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()));      

...

    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/

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

最佳答案

嗯@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 月开始的,假设您从这篇文章中得到了它(至少我做到了): SyndicationFeed: Content as CDATA?

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

关于wcf - Rss20FeedFormatter 忽略 SyndicateItem.Summary 的 TextSyndicateContent 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7204840/

相关文章:

c# - WCF/C# 无法捕获 EndpointNotFoundException

c# - WCF 错误序列化循环引用

c# - 在 WinForm C# 中获取最新的 xml (url)

java - 是否有一个支持良好的 Java 提要生成库?

c# - 聚合源 : How to access content:encoded?

c# - 从服务向 WPF 应用程序发送通知的好方法

java - 从 pubDate RSS 标签中提取日期

c# - 如何将 SyndicationElementExtension 添加到 SyndicationItem

c# - 无法解析/使用 System.ServiceModel.Security.WSTrustServiceContract 作为服务名称