SyndicationFeed: Content as CDATA?

前端 未结 9 2121
梦毁少年i
梦毁少年i 2021-01-13 16:17

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)

9条回答
  •  耶瑟儿~
    2021-01-13 16:56

    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);
        }
    }
    

提交回复
热议问题