Note that there are some explanatory texts on larger screens.

plurals
  1. PORss20FeedFormatter Ignores TextSyndicationContent type for SyndicationItem.Summary
    primarykey
    data
    text
    <p>While using the Rss20FeedFormatter class in a WCF project, I was trying to wrap the content of my description elements with a <code>&lt;![CDATA[ ]]&gt;</code> section. I found that no matter what I did, the HTML content of the description elements was always encoded and the CDATA section was never added. After peering into the source code of Rss20FeedFormatter, I found that when building the Summary node, it basically creates a new TextSyndicationContent instance which wipes out whatever settings were previously specified (<em>I think</em>).</p> <p><strong>My Code</strong></p> <pre><code>public class CDataSyndicationContent : TextSyndicationContent { public CDataSyndicationContent(TextSyndicationContent content) : base(content) { } protected override void WriteContentsTo(System.Xml.XmlWriter writer) { writer.WriteCData(Text); } } </code></pre> <p><em>... (The following code should wrap the Summary with a CDATA section)</em></p> <pre><code>SyndicationItem item = new SyndicationItem(); item.Title = new TextSyndicationContent(name); item.Summary = new CDataSyndicationContent( new TextSyndicationContent( "&lt;div&gt;This is a test&lt;/div&gt;", TextSyndicationContentKind.Html)); </code></pre> <p><strong>Rss20FeedFormatter Code</strong> <em>(AFAIK, the above code does not work because of this logic)</em></p> <pre><code>... else if (reader.IsStartElement("description", "")) result.Summary = new TextSyndicationContent(reader.ReadElementString()); ... </code></pre> <p>As a workaround, I've resorted to using the RSS20FeedFormatter to build the RSS, and then patch the RSS manually. For example:</p> <pre><code> StringBuilder buffer = new StringBuilder(); XmlTextWriter writer = new XmlTextWriter(new StringWriter(buffer)); feedFormatter.WriteTo(writer ); // feedFormatter = RSS20FeedFormatter PostProcessOutputBuffer(buffer); WebOperationContext.Current.OutgoingResponse.ContentType = "application/xml; charset=utf-8"; return new MemoryStream(Encoding.UTF8.GetBytes(buffer.ToString())); </code></pre> <p>...</p> <pre><code> public void PostProcessOutputBuffer(StringBuilder buffer) { var xmlDoc = XDocument.Parse(buffer.ToString()); foreach (var element in xmlDoc.Descendants("channel").First() .Descendants("item") .Descendants("description")) { VerifyCdataHtmlEncoding(buffer, element); } foreach (var element in xmlDoc.Descendants("channel").First() .Descendants("description")) { VerifyCdataHtmlEncoding(buffer, element); } buffer.Replace(" xmlns:a10=\"http://www.w3.org/2005/Atom\"", " xmlns:atom=\"http://www.w3.org/2005/Atom\""); buffer.Replace("a10:", "atom:"); } private static void VerifyCdataHtmlEncoding(StringBuilder buffer, XElement element) { if (!element.Value.Contains("&lt;") || !element.Value.Contains("&gt;")) { return; } var cdataValue = string.Format("&lt;{0}&gt;&lt;![CDATA[{1}]]&gt;&lt;/{2}&gt;", element.Name, element.Value, element.Name); buffer.Replace(element.ToString(), cdataValue); } </code></pre> <p>The idea for this workaround came from the following location, I just adapted it to work with WCF instead of MVC. <a href="http://localhost:8732/Design_Time_Addresses/SyndicationServiceLibrary1/Feed1/" rel="noreferrer">http://localhost:8732/Design_Time_Addresses/SyndicationServiceLibrary1/Feed1/</a></p> <p>I'm just wondering if this is simply a bug in Rss20FeedFormatter or is it by design? Also, if anyone has a better solution, I'd love to hear it!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload