繁体   English   中英

SyndicationFeed:内容为CDATA?

[英]SyndicationFeed: Content as CDATA?

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

这对我有用:

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

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

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

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

这应该工作。

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

我遇到的问题与在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);
    }
}

可能为时已晚,但我留下了解决方案。 我将它添加为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));

这是我们做的:

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

尝试这个

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

最简单的方法是:

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

这将在XML中输出

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

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

尝试

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

暂无
暂无

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

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