2009-07-13 69 views
4

我使用.NET的SyndicationFeed来创建RSS和ATOM提要。不幸的是,我需要描述元素(SyndicationItem的Content属性)中的HTML内容,格式化程序自动编码HTML,但我宁愿将整个描述元素包装在CDATA中,而不编码HTML。SyndicationFeed:作为CDATA的内容?

我的(简单)的代码:

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。

回答

7

这为我工作:

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

这也适用于我。 – thelsdj 2011-05-12 17:31:59

+0

伟大的解决方案,正是我所期待的。 – 2011-09-28 11:38:14

-2

尝试

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

thx,但不工作(编译器错误),因为item.Content需要一个SyndicationContent对象。 其他的方式,也不能工作,包括CDATA标签内容被编码: “<[+ ”项目内容“ +“] item.Content = SyndicationContent.CreateHtmlContent(!CDATA [”]> )“; – 2009-07-13 09:31:54

3

这应该工作。

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

SyndicationContent.CreateHtmlContent()包装上述所以结果是相同的,无论你使用什么。我相信当您尝试将提要写入XmlWriter时会出现问题。 – 2011-04-29 16:50:53

1

试试这个

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

我有同样的问题,因为一些地方的WriteContentsTo覆盖在cpowers的例子中没有被调用(仍然没有id为什么)。所以,我将它改为从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); 
    } 
} 
3

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

thelsdjAndy Rose,然后肯定的回答来看后来从TimLeung“否定”的响应和可替换的实施WonderGrub提供我估计,通过cpowers提供的固定停在ASP.NET的一些较新版本的工作或者其他的东西。

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

1

这是我们所做的事情:

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

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

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

将被输出在XML中作为

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

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