IN .NET 3.5我知道可以創建Atom 1.0和RSS 2.0 rss提要的System.ServiceModel.Syndication類。有誰知道在ASP.Net中輕鬆生成雅虎媒體RSS的方法嗎?我正在尋找一種免費且快速的方式來生成MRSS Feed。在ASP.NET 3.5中生成媒體RSS(MRSS)提要
3
A
回答
2
這裏簡化的解決方案,我結束了使用的HttpHandler內(ASHX):
public void GenerateRss(HttpContext context, IEnumerable<Media> medias)
{
context.Response.ContentType = "application/rss+xml";
XNamespace media = "http://search.yahoo.com/mrss";
List<Media> videos2xml = medias.ToList();
XDocument rss = new XDocument(
new XElement("rss", new XAttribute("version", "2.0"),
new XElement("channel",
new XElement("title", ""),
new XElement("link", ""),
new XElement("description", ""),
new XElement("language", ""),
new XElement("pubDate", DateTime.Now.ToString("r")),
new XElement("generator", "XLinq"),
from v in videos2xml
select new XElement("item",
new XElement("title", v.Title.Trim()),
new XElement("link", "",
new XAttribute("rel", "alternate"),
new XAttribute("type", "text/html"),
new XAttribute("href", String.Format("/Details.aspx?vid={0}", v.ID))),
new XElement("id", NotNull(v.ID)),
new XElement("pubDate", v.PublishDate.Value.ToLongDateString()),
new XElement("description",
new XCData(String.Format("<a href='/Details.aspx?vid={1}'> <img src='/Images/ThumbnailHandler.ashx?vid={1}' align='left' width='120' height='90' style='border: 2px solid #B9D3FE;'/></a><p>{0}</p>", v.Description, v.ID))),
new XElement("author", NotNull(v.Owner)),
new XElement("link",
new XAttribute("rel", "enclosure"),
new XAttribute("href", String.Format("/Details.aspx?vid={0}", v.ID)),
new XAttribute("type", "video/wmv")),
new XElement(XName.Get("title", "http://search.yahoo.com/mrss"), v.Title.Trim()),
new XElement(XName.Get("thumbnail", "http://search.yahoo.com/mrss"), "",
new XAttribute("url", String.Format("/Images/ThumbnailHandler.ashx?vid={0}", v.ID)),
new XAttribute("width", "320"),
new XAttribute("height", "240")),
new XElement(XName.Get("content", "http://search.yahoo.com/mrss"), "a",
new XAttribute("url", String.Format("/Details.aspx?vid={0}", v.ID)),
new XAttribute("fileSize", Default(v.FileSize)),
new XAttribute("type", "video/wmv"),
new XAttribute("height", Default(v.Height)),
new XAttribute("width", Default(v.Width))
)
)
)
)
);
using (XmlWriter writer = new XmlTextWriter(context.Response.OutputStream, Encoding.UTF8))
{
try
{
rss.WriteTo(writer);
}
catch (Exception ex)
{
Log.LogError("VideoRss", "GenerateRss", ex);
}
}
}
1
第1步:您的數據轉換成XML:
所以,對於一個列表圖片:
var photoXml = new XElement("photos",
new XElement("album",
new XAttribute("albumId", albumId),
new XAttribute("albumName", albumName),
new XAttribute("modified", DateTime.Now.ToUniversalTime().ToString("r")),
from p in photos
select
new XElement("photo",
new XElement("id", p.PhotoID),
new XElement("caption", p.Caption),
new XElement("tags", p.StringTags),
new XElement("albumId", p.AlbumID),
new XElement("albumName", p.AlbumName)
) // Close element photo
) // Close element album
);// Close element photos
第2步:通過一些XSLT運行XML:
然後使用類似的接下來,運行到some XSLT,其中xslPath是XSLT的路徑,current是當前的HttpContext:
var xt = new XslCompiledTransform();
xt.Load(xslPath);
var ms = new MemoryStream();
if (null != current){
var xslArgs = new XsltArgumentList();
var xh = new XslHelpers(current);
xslArgs.AddExtensionObject("urn:xh", xh);
xt.Transform(photoXml.CreateNavigator(), xslArgs, ms);
} else {
xt.Transform(photoXml.CreateNavigator(), null, ms);
}
// Set the position to the beginning of the stream.
ms.Seek(0, SeekOrigin.Begin);
// Read the bytes from the stream.
var byteArray = new byte[ms.Length];
ms.Read(byteArray, 0, byteArray.Length);
// Decode the byte array into a char array
var uniEncoding = new UTF8Encoding();
var charArray = new char[uniEncoding.GetCharCount(
byteArray, 0, byteArray.Length)];
uniEncoding.GetDecoder().GetChars(
byteArray, 0, byteArray.Length, charArray, 0);
var sb = new StringBuilder();
sb.Append(charArray);
// Returns the XML as a string
return sb.ToString();
我有兩位代碼坐在一個方法「BuildPhotoStream」。
類「XslHelpers」包含以下內容:
public class XslHelpers{
private readonly HttpContext current;
public XslHelpers(HttpContext currentContext){
current = currentContext;
}
public String ConvertDateTo822(string dateTime){
DateTime original = DateTime.Parse(dateTime);
return original.ToUniversalTime()
.ToString("ddd, dd MMM yyyy HH:mm:ss G\"M\"T");
}
public String ServerName(){
return current.Request.ServerVariables["Server_Name"];
}
}
基本上,這爲我提供了日期的一些不錯的格式是XSLT好好嘗試一下給我。
步驟3:使所得的XML返回到客戶端應用程序:
「BuildPhotoStream」是由「RenderHelpers.Photos」和「RenderHelpers.LatestPhotos」,這是負責從LINQ2SQL獲取照片細節稱爲對象,他們是從空aspx頁面調用(現在我知道,這確實應該是一個ASHX處理程序,我只是沒有得到解決,以修復它):
protected void Page_Load(object sender, EventArgs e)
{
Response.ContentType = "application/rss+xml";
ResponseEncoding = "UTF-8";
if (!string.IsNullOrEmpty(Request.QueryString["AlbumID"]))
{
Controls.Add(new LiteralControl(RenderHelpers
.Photos(Server.MapPath("/xsl/rssPhotos.xslt"), Context)));
}
else
{
Controls.Add(new LiteralControl(RenderHelpers
.LatestPhotos(Server.MapPath("/xsl/rssLatestPhotos.xslt"), Context)));
}
}
在所有的結束,我最終與此:
其中在Cooliris的/ PicLens工作時,我設置它,但是現在似乎呈現在反射面的圖片,當你點擊它們,而不是在壁上觀:(
如果你錯過了上述情況,XSLT可以在這裏找到:
你顯然需要編輯它以適應你的需求(並且在Visual Studio中打開它 - FF隱藏大部分樣式表def,包括xmlns:atom =「http://www.w3.org/2005/Atom 「xmlns:media =」http://search.yahoo.com/mrss「)。
1
我能夠通過執行以下向媒體命名空間添加了RSS標籤:
XmlDocument feedDoc = new XmlDocument();
feedDoc.Load(new StringReader(xmlText));
XmlNode rssNode = feedDoc.DocumentElement;
// You cant directly set the attribute to anything other then then next line. So you have to set the attribute value on a seperate line.
XmlAttribute mediaAttribute = feedDoc.CreateAttribute("xmlns", "media", "http://www.w3.org/2000/xmlns/");
mediaAttribute.Value = "http://search.yahoo.com/mrss/";
rssNode.Attributes.Append(mediaAttribute);
return feedDoc.OuterXml;
0
相關問題
- 1. 在Rails 3中生成RSS提要
- 2. Django使用描述生成RSS提要
- 3. RSS提要的生成和實現
- 4. 快速生成/發佈來自ASP.NET項目的RSS提要?
- 5. RSS提要在ASP.NET中回傳消失
- 6. 如何在ASP.NET 3.5中創建RSS源?
- 7. 在ASP.NET 3.5中創建RSS源
- 8. ASP.net 3.5網站需要流媒體視頻
- 9. RSS XML解析的問題(如何從RSS提要媒體內容的價值?)
- 10. 顯示mrss內容與由mrss作爲wordpress帖子提供的所有類型的媒體
- 11. asp.net mvc中的RSS提要異常
- 12. 如何生成RSS提要並提供給用戶
- 13. 如何覆蓋DSpace中RSS提要的生成?
- 14. 從鏈接列表中即時生成RSS提要?
- 15. ColdFusion生成的rss提要中的格式描述
- 16. 在RSS提要PHP
- 17. 使用asp.net解析RSS提要c#
- 18. RSS提要
- 19. Youtube RSS提要
- 20. 從RSS提要
- 21. 用XML製作的SyndicationFeedFormatter生成定製的RSS/Atom提要
- 22. 如何生成合並的RSS提要(來自DokuWiki和Serendipity)
- 23. 爲Twitter rss提要生成緩存文件
- 24. 什麼是好的scala庫來生成RSS提要?
- 25. 獲取RSS提要時將MediaEntryModule設置爲null。無法獲取媒體內容
- 26. 在iPhone上生成PDF,媒體大小的PDF生成器
- 27. excel中的RSS提要
- 28. 自動將RSS提要提交至RSS提要目錄
- 29. 如何創建RSS提要?
- 30. 在C#中的RSS提要的最佳實現(ASP.net)
其實我有我的團隊對這個工作的人,試圖利用內置的SyndicationFeed類。他只是向我報告,他相信他找到了解決方案,希望我能很快發佈一個解決方案。 – James 2009-05-29 12:25:14
這將是偉大的請帖 – chrisg 2009-05-29 14:49:31