Trong khi sử dụng lớp Rss20FeedFormatter trong một dự án WCF, tôi đã cố gắng bọc nội dung của các phần tử mô tả của mình với một phần <![CDATA[ ]]>
. Tôi thấy rằng không có vấn đề gì tôi đã làm, nội dung HTML của các yếu tố mô tả luôn được mã hóa và phần CDATA chưa bao giờ được thêm vào. Sau khi nhìn vào mã nguồn của Rss20FeedFormatter, tôi thấy rằng khi xây dựng nút Tóm tắt, về cơ bản nó tạo ra một cá thể TextSyndicationContent mới xóa sạch mọi cài đặt đã được chỉ định trước đó (Tôi nghĩ).Rss20FeedFormatter Bỏ qua TextSyndicationLoại nội dung cho SyndicationItem.Summary
Mã My
public class CDataSyndicationContent : TextSyndicationContent
{
public CDataSyndicationContent(TextSyndicationContent content)
: base(content)
{
}
protected override void WriteContentsTo(System.Xml.XmlWriter writer)
{
writer.WriteCData(Text);
}
}
... (Các mã sau đây nên quấn Tóm tắt thông tin với một phần CDATA)
SyndicationItem item = new SyndicationItem();
item.Title = new TextSyndicationContent(name);
item.Summary = new CDataSyndicationContent(
new TextSyndicationContent(
"<div>This is a test</div>",
TextSyndicationContentKind.Html));
Rss20FeedFormatter Mã (AFAIK, các mã trên không hoạt động vì logic này)
...
else if (reader.IsStartElement("description", ""))
result.Summary = new TextSyndicationContent(reader.ReadElementString());
...
Giải pháp thay thế, tôi đã sử dụng RSS20FeedFormatter để tạo RSS và sau đó vá RSS theo cách thủ công. Ví dụ:
StringBuilder buffer = new StringBuilder();
XmlTextWriter writer = new XmlTextWriter(new StringWriter(buffer));
feedFormatter.WriteTo(writer); // feedFormatter = RSS20FeedFormatter
PostProcessOutputBuffer(buffer);
WebOperationContext.Current.OutgoingResponse.ContentType =
"application/xml; charset=utf-8";
return new MemoryStream(Encoding.UTF8.GetBytes(buffer.ToString()));
...
public void PostProcessOutputBuffer(StringBuilder buffer)
{
var xmlDoc = XDocument.Parse(buffer.ToString());
foreach (var element in xmlDoc.Descendants("channel").First()
.Descendants("item")
.Descendants("description"))
{
VerifyCdataHtmlEncoding(buffer, element);
}
foreach (var element in xmlDoc.Descendants("channel").First()
.Descendants("description"))
{
VerifyCdataHtmlEncoding(buffer, element);
}
buffer.Replace(" xmlns:a10=\"http://www.w3.org/2005/Atom\"",
" xmlns:atom=\"http://www.w3.org/2005/Atom\"");
buffer.Replace("a10:", "atom:");
}
private static void VerifyCdataHtmlEncoding(StringBuilder buffer,
XElement element)
{
if (!element.Value.Contains("<") || !element.Value.Contains(">"))
{
return;
}
var cdataValue = string.Format("<{0}><![CDATA[{1}]]></{2}>",
element.Name,
element.Value,
element.Name);
buffer.Replace(element.ToString(), cdataValue);
}
Ý tưởng cho việc này xuất phát từ vị trí sau, tôi chỉ thích nghi nó để làm việc với WCF thay vì MVC. http://localhost:8732/Design_Time_Addresses/SyndicationServiceLibrary1/Feed1/
Tôi chỉ tự hỏi liệu đây có phải là lỗi trong Rss20FeedFormatter hay là do thiết kế? Ngoài ra, nếu có ai có giải pháp tốt hơn, tôi rất muốn nghe nó!