Reputation: 567
I am adding some simple tags to my SyndicationItem Content object like
"<p>Hello World</p>"
but it is shown in the RSS feed as
"<p>Hello World</p>"
I have tried to different ways to avoid this, but none works.
Upvotes: 2
Views: 758
Reputation: 1917
I have seen a number of solutions offered that work with old versions of .net here :
SyndicationFeed: Content as CDATA?
As well as a post processing solution here :
https://gist.github.com/davidwhitney/1027181
The first solution was not viable for me since we are using the latest version of .net, and the second looked like a lot of overhead for something so simple, so I ended up implementing this solution :
public class CDataWriter : XmlTextWriter
{
public CDataWriter(TextWriter w) : base(w) { }
public CDataWriter(Stream w, Encoding encoding) : base(w, encoding) { }
public CDataWriter(string filename, Encoding encoding) : base(filename, encoding) { }
public override void WriteString(string text)
{
if (text.Contains("<") && text.Contains(">"))
{
base.WriteCData(text);
}
else
{
base.WriteString(text);
}
}
}
And then to use the class :
public static class FeedToStringBuilder
{
public static StringBuilder CDataOverwriteMethod(Rss20FeedFormatter formatter)
{
var buffer = new StringBuilder();
var xmlWriterSettings = new XmlWriterSettings {Indent = true};
using (var stream = new StringWriter(buffer))
using (var writer = new CDataWriter(stream))
using (var xmlWriter = XmlWriter.Create(writer, xmlWriterSettings))
{
formatter.WriteTo(xmlWriter);
}
return buffer;
}
}
Using in a controller:
public ActionResult RssFeed()
{
SyndicationFeed feed;
using (var dataContext = new DataContext())
{
string urlBase = $"{Request.Url.Scheme}://{Request.Url.Authority}{Request.ApplicationPath.TrimEnd('/')}/";
feed = new PodcastFeed(dataContext, Server).GetFeed(urlBase);
}
var rss20FeedFormatter = new Rss20FeedFormatter(feed);
rss20FeedFormatter.SerializeExtensionsAsAtom = false;
var feedXml = FeedToStringBuilder.CDataOverwriteMethod(rss20FeedFormatter).ToString();
return Content(feedXml, "text/xml");
}
Upvotes: 1