我正在使用.NET的SyndicationFeed创建RSS和ATOM提要。不幸的是,我需要在description元素(SyndicationItem的content属性)中包含HTML内容,并且格式化程序会自动对其进行编码,但我宁愿将整个description元素包装在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做到这一点吗?我最后的办法是“手动”创建提要的XML,但我更愿意使用内置的SyndicationFeed。
发布于 2011-02-05 12:11:35
这对我很有效:
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))发布于 2013-05-24 06:52:08
对于那些cpowers和WonderGrub提供的解决方案也不起作用的人,你应该检查下面的SO问题,因为对我来说,这个问题实际上是我遇到这个问题的答案!Rss20FeedFormatter Ignores TextSyndicationContent type for SyndicationItem.Summary
从thelsdj和Andy Rose的肯定回答,以及后来TimLeung的“否定”回答和WonderGrub提供的替代方案来看,我估计cpowers提供的修复程序在某些较新版本的ASP.NET或其他版本中停止工作。
在任何情况下,上面SO文章中的解决方案(源自David Whitney的代码)为我解决了RSS2.0提要中CDATA块中不需要的HTML编码的问题。我在一个ASP.NET 4.0 WebForms应用程序中使用了它。
发布于 2010-03-11 04:33:38
这应该是可行的。
item.Content = new TextSyndicationContent("<b>Item Content</b>",TextSyndicationContentKind.Html);https://stackoverflow.com/questions/1118409
复制相似问题