-->

SyndicationFeed:内容CDATA?(SyndicationFeed: Content

2019-06-25 17:20发布

我使用.NET的SyndicationFeed创建RSS和ATOM供稿。 不幸的是,我需要在描述元素的HTML内容(SyndicationItem的内容属性)和格式自动编码的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? 我的最后一招是“手工”创建,原料中的XML,但我宁愿使用内置的SyndicationFeed。

Answer 1:

这为我工作:

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


Answer 2:

对于那些对他们来说,通过cpowers和WonderGrub提供的解决方案还没有工作,你应该看看下面的SO问题,因为对我来说这个问题实际上是回答我这个问题的occurence! Rss20FeedFormatter忽略了SyndicationItem.Summary TextSyndicationContent类型

从正面的回答来看thelsdjAndy Rose再后来从“消极的”响应TimLeung和提供替代WonderGrub我估计,通过cpowers提供的固定停在ASP.NET或某事的一些较新版本的工作。

在任何情况下,在SO上述文章中(从大卫惠特尼的代码导出的)的溶液来解决与CDATA阻挡不需要的HTML编码的问题在RSS 2.0订阅我。 我用它在ASP.NET 4.0 Web窗体应用程序。



Answer 3:

这应该工作。

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


Answer 4:

我有同样的问题,因为一些在WriteContentsTo覆盖没有被称为cpowers例如(仍然不知道为什么)。 所以,我改变了它从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);
    }
}


Answer 5:

这可能是为时已晚,但我离开我的解决方案。 我添加它作为一个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));


Answer 6:

试试这个

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


Answer 7:

这是我们所做的事情:

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


Answer 8:

要做到这一点最简单的办法是:

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

这将在XML作为输出

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

不是一个完美的解决方案,我承认,但它工作正常 - 只是试穿了我的一个项目。



Answer 9:

尝试

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


文章来源: SyndicationFeed: Content as CDATA?