首页 文章

SyndicationFeed:内容为CDATA?

提问于
浏览
4

我正在使用.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 .

9 回答

  • 4

    这对我有用:

    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))
    
  • 1

    对于那些由cpowers和WonderGrub提供的解决方案也不起作用的人,你应该检查以下SO问题,因为对我来说这个问题实际上是我出现这个问题的答案! Rss20FeedFormatter Ignores TextSyndicationContent type for SyndicationItem.Summary

    thelsdjAndy Rose 的正面答案以及后来 TimLeung 的'negative'响应以及 WonderGrub 提供的替代方案来判断,我估计cpowers提供的修复程序在某些更高版本的ASP.NET或其他版本中停止工作 .

    在任何情况下,上述SO文章中的解决方案(源自David Whitney的代码)解决了RSS 2.0 Feed中CDATA块中不需要的HTML编码问题 . 我在ASP.NET 4.0 WebForms应用程序中使用它 .

  • -3

    这应该工作 .

    item.Content =  new TextSyndicationContent("<b>Item Content</b>",TextSyndicationContentKind.Html);
    
  • 3

    我遇到的问题与在cpowers示例中没有调用WriteContentsTo覆盖的情况相同(仍然不知道为什么) . 因此,我将其更改为继承SyndicationContent类 . 不确定这是否是最好的解决方案,但在我的情况下工作得很好 .

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

    试试这个

    XmlReaderSettings settings = new XmlReaderSettings();
                settings.IgnoreComments = false;
                //settings.ProhibitDtd = false;
                using (XmlReader reader = XmlReader.Create(rssurl, settings))
    
  • 1

    这是我们做的:

    public class XmlCDataWriter : XmlTextWriter
           {
               public XmlCDataWriter(TextWriter w): base(w){}
    
               public XmlCDataWriter(Stream w, Encoding encoding): base(w, encoding){}
    
               public XmlCDataWriter(string filename, Encoding encoding): base(filename, encoding){}
    
               public override void WriteString(string text)
               {
                   if (text.Contains("<"))
                   {
                       base.WriteCData(text);
                   }
                   else
                   {
                       base.WriteString(text);
                   }
               }
    
           }
    

    然后使用该类:

    public StringBuilder CDataOverwiriteMethod(Rss20FeedFormatter formatter)
           {
               var buffer = new StringBuilder();
    
               //could be streamwriter as well
               using (var stream = new StringWriter(buffer))
               {
                   using (var writer = new XmlCDataWriter(stream))
                   {
                       var settings = new XmlWriterSettings() {Indent = true};
    
                       using (var xmlWriter = XmlWriter.Create(writer, settings))
                       {
                           formatter.WriteTo(xmlWriter);
                       }
                   }
               }
    
               return buffer;
           }
    
  • 1

    可能为时已晚,但我留下了解决方案 . 我将它添加为ElementExtension然后它适用于我 . 我的环境是.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));
    
  • 8

    最简单的方法是:

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

    这将在XML中输出

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

    我承认,这不是一个优雅的解决方案,但它运作正常 - 只是尝试了我的项目 .

  • 0

    尝试

    item.Content = "<![CDATA[" + 
                SyndicationContent.CreateHtmlContent("<b>Item Content</b>") + "]]>";
    

相关问题