SyndicationFeed: Content as CDATA?

2019-02-18 06:36发布

I'm using .NET's SyndicationFeed to create RSS and ATOM feeds. Unfortunately, I need HTML content in the description element (the Content property of the SyndicationItem) and the formatter automatically encodes the HTML, but I'd rather have the entire description element wrapped in CDATA without encoding the HTML.

My (simple) code:

var feed = new SyndicationFeed("Title", "Description", 
               new Uri("http://someuri.com"));
var items = new List<SyndicationItem>();

var item = new SyndicationItem("Item Title", (string)null, 
               new Uri("http://someitemuri.com"));

item.Content = SyndicationContent.CreateHtmlContent("<b>Item Content</b>");

items.Add(item);
feed.Items = items;

Anybody an idea how I can do this using SyndicationFeed? My last resort is to "manually" create the XML for the feeds, but I'd rather use the built-in SyndicationFeed.

9条回答
劫难
2楼-- · 2019-02-18 07:06

The shortest way to do this is:

.Content = SyndicationContent.CreateXhtmlContent("<![CDATA[The <em>content</em>]]>")

That will be outputted in the XML as

<entry>
  …
  <content type="xhtml"><![CDATA[The <em>content</em>]]></content>
  …
</entry>

Not an elegant solution, I admit, but it works properly – just tried on a project of mine.

查看更多
别忘想泡老子
3楼-- · 2019-02-18 07:08

For those for whom the solution provided by cpowers and WonderGrub also didn't work, you should check out the following SO question, because for me this question was actually the answer to my occurence of this problem! Rss20FeedFormatter Ignores TextSyndicationContent type for SyndicationItem.Summary

Judging from the positive answer from thelsdj and Andy Rose and then later the 'negative' response from TimLeung and the alternative offered by WonderGrub I would estimate that the fix offered by cpowers stopped working in some later version of ASP.NET or something.

In any case the solution in the above SO article (derived from David Whitney's code) solved the problem with unwanted HTML encoding in CDATA blocks in an RSS 2.0 feed for me. I used it in an ASP.NET 4.0 WebForms application.

查看更多
ら.Afraid
4楼-- · 2019-02-18 07:09

try

item.Content = "<![CDATA[" + 
            SyndicationContent.CreateHtmlContent("<b>Item Content</b>") + "]]>";
查看更多
该账号已被封号
5楼-- · 2019-02-18 07:11

I had the same problem as some where the WriteContentsTo override wasn't being called in cpowers example (still no idea why). So, I changed it to inherit from the SyndicationContent class instead. Not sure if this is the best solution, but worked great in my situation.

public class CDataSyndicationContent : SyndicationContent
{
    public CDataSyndicationContent(string content)
    {
        Text = content;
    }

    public override SyndicationContent Clone()
    {
        return new CDataSyndicationContent(Text);
    }

    public override string Type
    {
        get { return "html"; }
    }

    public string Text { get; private set; }

    protected override void WriteContentsTo(XmlWriter writer)
    {
        writer.WriteCData(Text);
    }
}
查看更多
Deceive 欺骗
6楼-- · 2019-02-18 07:16

This should work.

item.Content =  new TextSyndicationContent("<b>Item Content</b>",TextSyndicationContentKind.Html);
查看更多
干净又极端
7楼-- · 2019-02-18 07:20

It might be too late but I leave my solution. I added it as a ElementExtension then it works for me. My environment is .NET 4.5.

XNamespace nsDefault = "http://www.w3.org/2005/Atom";
var content = new XElement(nsDefault + "content");
content.Add(new XCData("<b>Item Content</b>"));
item.ElementExtensions.Add(new SyndicationElementExtension(content));
查看更多
登录 后发表回答