2009-07-13 43 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> 

我承認,這不是一個優雅的解決方案,但它正常運行 - 只是在我的一個項目上嘗試過。