SyndicationFeed: Content as CDATA?

前端 未结 9 2067
梦毁少年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:35

    This should work.

    item.Content =  new TextSyndicationContent("<b>Item Content</b>",TextSyndicationContentKind.Html);
    
    0 讨论(0)
  • 2021-01-13 16:35

    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));
    
    0 讨论(0)
  • 2021-01-13 16:38

    This worked for me:

    public class CDataSyndicationContent : TextSyndicationContent
    {
        public CDataSyndicationContent(TextSyndicationContent content)
            : base(content)
        {}
    
        protected override void  WriteContentsTo(System.Xml.XmlWriter writer)
        {
            writer.WriteCData(Text);
        }
    }
    

    then you can:

    new CDataSyndicationContent(new TextSyndicationContent(content, TextSyndicationContentKind.Html))
    
    0 讨论(0)
  • 2021-01-13 16:38

    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.

    0 讨论(0)
  • 2021-01-13 16:39

    Here is what we did :

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

    And then to use the class :

    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;
           }
    
    0 讨论(0)
  • 2021-01-13 16:52

    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.

    0 讨论(0)
提交回复
热议问题