在.NET 3.5中,我知道可以创建Atom 1.0和RSS 2.0 rss源的System.ServiceModel.Syndication类。有谁知道在ASP.Net中轻松生成雅虎媒体RSS的方法?我正在寻找一种快速,快捷的方式来生成MRSS饲料。
答案 0 :(得分: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)
第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“)。
答案 2 :(得分: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;
答案 3 :(得分:0)