简体   繁体   English

SyndicationFeed:内容为CDATA?

[英]SyndicationFeed: Content as CDATA?

I'm using .NET's SyndicationFeed to create RSS and ATOM feeds. 我正在使用.NET的SyndicationFeed来创建RSS和ATOM提要。 Unfortunately, I need HTML content in the description element (the Content property of the SyndicationItem) and the formatter automatically encodes the HTML, but I'd rather have the entire description element wrapped in CDATA without encoding the HTML. 不幸的是,我需要在描述元素(SyndicationItem的Content属性)中使用HTML内容,格式化程序会自动对HTML进行编码,但我宁愿将整个描述元素包装在CDATA中,而不对HTML进行编码。

My (simple) code: 我的(简单)代码:

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;

Anybody an idea how I can do this using SyndicationFeed? 有人知道如何使用SyndicationFeed做到这一点吗? My last resort is to "manually" create the XML for the feeds, but I'd rather use the built-in SyndicationFeed. 我的最后一招是“手动”为feed创建XML,但我宁愿使用内置的SyndicationFeed。

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

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! 对于那些由cpowers和WonderGrub提供的解决方案也不起作用的人,你应该查看以下SO问题,因为对我来说这个问题实际上是我出现这个问题的答案! Rss20FeedFormatter Ignores TextSyndicationContent type for SyndicationItem.Summary Rss20FeedFormatter忽略SyndicationItem.Summary的TextSyndicationContent类型

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. thelsdjAndy Rose的积极回答以及后来thelsdj的“负面”回应以及TimLeung提供的替代方案WonderGrub我估计cpowers提供的修复程序在某些更高版本的ASP.NET中停止工作。

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. 在任何情况下,上述SO文章中的解决方案(源自David Whitney的代码)解决了RSS 2.0 Feed中CDATA块中不需要的HTML编码的问题。 I used it in an ASP.NET 4.0 WebForms application. 我在ASP.NET 4.0 WebForms应用程序中使用它。

这应该工作。

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

I had the same problem as some where the WriteContentsTo override wasn't being called in cpowers example (still no idea why). 我遇到的问题与在cpowers示例中没有调用WriteContentsTo覆盖的情况相同(仍然不知道为什么)。 So, I changed it to inherit from the SyndicationContent class instead. 因此,我将其更改为继承SyndicationContent类。 Not sure if this is the best solution, but worked great in my situation. 不确定这是否是最好的解决方案,但在我的情况下工作得很好。

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

It might be too late but I leave my solution. 可能为时已晚,但我留下了解决方案。 I added it as a ElementExtension then it works for me. 我将它添加为ElementExtension然后它适用于我。 My environment is .NET 4.5. 我的环境是.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));

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

try this 尝试这个

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

The shortest way to do this is: 最简单的方法是:

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

That will be outputted in the XML as 这将在XML中输出

<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. 我承认,这不是一个优雅的解决方案,但它运作正常 - 只是尝试了我的项目。

try 尝试

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM