我正在使用.NET的SyndicationFeed来创建RSS和ATOM提要.不幸的是,我需要在描述元素(SyndicationItem的Content属性)中使用
HTML内容,格式化程序会自动对
HTML进行编码,但我宁愿将整个描述元素包装在CDATA中,而不对HTML进行编码.
我的(简单)代码:
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;
有人知道如何使用SyndicationFeed做到这一点吗?我的最后一招是“手动”为Feed创建XML,但我宁愿使用内置的SyndicationFeed.
解决方法
这对我有用:
public class CDataSyndicationContent : TextSyndicationContent { public CDataSyndicationContent(TextSyndicationContent content) : base(content) {} protected override void WriteContentsTo(System.Xml.XmlWriter writer) { writer.WriteCData(Text); } }
然后你可以:
new CDataSyndicationContent(new TextSyndicationContent(content,TextSyndicationContentKind.Html))