在ASP中生成媒体RSS (MRSS)提要。NET 3.5

时间:2021-12-18 00:59:36

IN .NET 3.5 I know of the System.ServiceModel.Syndication classes which can create Atom 1.0 and RSS 2.0 rss feeds. Does anyone know of a way to easily generate yahoo's Media RSS in ASP.Net? I am looking for a free and fast way to generate an MRSS feed.

在。net 3.5中,我知道System.ServiceModel。可以创建Atom 1.0和RSS 2.0提要的联合类。有没有人知道在ASP.Net中轻松生成雅虎的媒体RSS ?我正在寻找一种免费和快速的方法来生成MRSS提要。

4 个解决方案

#1


2  

Here a simplified solution I ended up using within a HttpHandler (ashx):

这里我在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);
            }
        }

    }

#2


1  

Step 1: Convert your data into XML:

步骤1:将数据转换为XML:

So, given a List photos:

因此,给出一张列表照片:

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

Step 2: Run the XML through some XSLT:

步骤2:通过一些XSLT运行XML:

Then using something like the following, run that through some XSLT, where xslPath is the path to your XSLT, current is the current HttpContext:

然后使用如下内容,通过一些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();

I have those two bits of code sitting in one method "BuildPhotoStream".

我在一个方法“BuildPhotoStream”中有这两段代码。

The class "XslHelpers" contains the following:

类“xslhelper”包含以下内容:

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"];
  }
}

This basically provides me with some nice formatting of dates that XSLT doens't give me.

这基本上为我提供了XSLT没有提供的日期格式。

Step 3: Render the resulting XML back to the client application:

步骤3:将得到的XML呈现回客户端应用程序:

"BuildPhotoStream" is called by "RenderHelpers.Photos" and "RenderHelpers.LatestPhotos", which are responsible for getting the photo details from the Linq2Sql objects, and they are called from an empty aspx page (I know now that this should really be an ASHX handler, I've just not gotten around to fixing it):

“BuildPhotoStream”被称为“renderhelper”。照片”和“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)));
    }
}

At the end of all that, I end up with this:

在这一切结束的时候,我的结论是:

http://www.doodle.co.uk/Albums/Rss.aspx?AlbumID=61

http://www.doodle.co.uk/Albums/Rss.aspx?AlbumID=61

Which worked in Cooliris/PicLens when I set it up, however now seems to render the images in the reflection plane, and when you click on them, but not in the wall view :(

它在Cooliris/PicLens中起作用当我设置它的时候,但是现在看起来在反射平面中呈现图像,当你点击它们时,但不是在wall视图中:

In case you missed it above, the XSLT can be found here:

如果您在上面漏掉了它,可以在这里找到XSLT:

http://www.doodle.co.uk/xsl/rssPhotos.xslt

http://www.doodle.co.uk/xsl/rssPhotos.xslt

You'll obviously need to edit it to suit your needs (and open it in something like Visual Studio - FF hides most of the stylesheet def, including xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss").

显然,您需要编辑它以满足您的需要(并在Visual Studio中打开它——FF隐藏了大部分样式表def,包括xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss")。

#3


1  

I was able to add the media namespace to the rss tag by doing the following:

我可以通过以下操作将媒体名称空间添加到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;

#4


0  

Just to add another option for future reference:

为以后的参考添加另一个选项:

I created a library that leverages the SyndicationFeed classes in .NET and allows you to read and write media rss feeds.

我创建了一个库,它利用。net中的SyndicationFeed类,允许您阅读和编写媒体rss提要。

http://mediarss.codeplex.com

http://mediarss.codeplex.com

#1


2  

Here a simplified solution I ended up using within a HttpHandler (ashx):

这里我在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);
            }
        }

    }

#2


1  

Step 1: Convert your data into XML:

步骤1:将数据转换为XML:

So, given a List photos:

因此,给出一张列表照片:

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

Step 2: Run the XML through some XSLT:

步骤2:通过一些XSLT运行XML:

Then using something like the following, run that through some XSLT, where xslPath is the path to your XSLT, current is the current HttpContext:

然后使用如下内容,通过一些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();

I have those two bits of code sitting in one method "BuildPhotoStream".

我在一个方法“BuildPhotoStream”中有这两段代码。

The class "XslHelpers" contains the following:

类“xslhelper”包含以下内容:

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"];
  }
}

This basically provides me with some nice formatting of dates that XSLT doens't give me.

这基本上为我提供了XSLT没有提供的日期格式。

Step 3: Render the resulting XML back to the client application:

步骤3:将得到的XML呈现回客户端应用程序:

"BuildPhotoStream" is called by "RenderHelpers.Photos" and "RenderHelpers.LatestPhotos", which are responsible for getting the photo details from the Linq2Sql objects, and they are called from an empty aspx page (I know now that this should really be an ASHX handler, I've just not gotten around to fixing it):

“BuildPhotoStream”被称为“renderhelper”。照片”和“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)));
    }
}

At the end of all that, I end up with this:

在这一切结束的时候,我的结论是:

http://www.doodle.co.uk/Albums/Rss.aspx?AlbumID=61

http://www.doodle.co.uk/Albums/Rss.aspx?AlbumID=61

Which worked in Cooliris/PicLens when I set it up, however now seems to render the images in the reflection plane, and when you click on them, but not in the wall view :(

它在Cooliris/PicLens中起作用当我设置它的时候,但是现在看起来在反射平面中呈现图像,当你点击它们时,但不是在wall视图中:

In case you missed it above, the XSLT can be found here:

如果您在上面漏掉了它,可以在这里找到XSLT:

http://www.doodle.co.uk/xsl/rssPhotos.xslt

http://www.doodle.co.uk/xsl/rssPhotos.xslt

You'll obviously need to edit it to suit your needs (and open it in something like Visual Studio - FF hides most of the stylesheet def, including xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss").

显然,您需要编辑它以满足您的需要(并在Visual Studio中打开它——FF隐藏了大部分样式表def,包括xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss")。

#3


1  

I was able to add the media namespace to the rss tag by doing the following:

我可以通过以下操作将媒体名称空间添加到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;

#4


0  

Just to add another option for future reference:

为以后的参考添加另一个选项:

I created a library that leverages the SyndicationFeed classes in .NET and allows you to read and write media rss feeds.

我创建了一个库,它利用。net中的SyndicationFeed类,允许您阅读和编写媒体rss提要。

http://mediarss.codeplex.com

http://mediarss.codeplex.com