如何将图像添加到asp.net rss feed

时间:2011-08-07 16:12:48

标签: asp.net rss

我是asp.net中rss feed的新手,但我在c#中修改xml的速度非常快。我想在rss2.0中添加一个图像。谢谢你的帮助。

Response.Clear();

        Response.ContentType = "text/xml";

        XmlTextWriter xtwFeed = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);

        xtwFeed.WriteStartDocument();

        // The mandatory rss tag

        xtwFeed.WriteStartElement("rss");

        xtwFeed.WriteAttributeString("version", "2.0");

        // The channel tag contains RSS feed details

        xtwFeed.WriteStartElement("channel");

        xtwFeed.WriteElementString("title", "The Latest goole RSS Feeds. Subscribe Today.");

        xtwFeed.WriteElementString("link", "http://googel.com");

        xtwFeed.WriteElementString("image", "http://google.com");

        xtwFeed.WriteElementString("description", "Click on the title to leave a comment.");

        xtwFeed.WriteElementString("copyright", "Copyright 2011 google.com. All rights reserved.");
        List<Blog> blogs = (List<Blog>) Blog.GetBlogs();
        foreach (var blog in blogs)
        {
            xtwFeed.WriteStartElement("item");

            xtwFeed.WriteElementString("title", blog.Title);

            xtwFeed.WriteElementString("link",blog.BlogURL);

            if(blog.PictureURL != null || blog.PictureURL != "")
            {

//想在这里添加图片                     xtwFeed.WriteElementString(“image”,blog.PictureURL);

            }
            xtwFeed.WriteElementString("description", blog.OutputMessage);

            xtwFeed.WriteElementString("copyright", "Copyright 2011 google.com. All rights reserved.");
            xtwFeed.WriteEndElement();
        }
        xtwFeed.WriteEndElement();

        xtwFeed.WriteEndElement();

        xtwFeed.WriteEndDocument();

        xtwFeed.Flush();

        xtwFeed.Close();

        Response.End();

编辑注意:我现在有正确的格式,但图像没有显示

 if(!string.IsNullOrEmpty(blog.PictureURL))
            {
                xtwFeed.WriteStartElement("image");
                xtwFeed.WriteElementString("url", blog.PictureURL);
                xtwFeed.WriteElementString("title", blog.Title);
                xtwFeed.WriteElementString("link", blog.BlogURL);
                xtwFeed.WriteEndElement();
            }

1 个答案:

答案 0 :(得分:1)

试试这个:

xtwFeed.WriteStartElement("enclosure");
 xtwFeed.WriteElementString("url", blog.PictureURL);
 xtwFeed.WriteElementString("type", image/jpeg);
 xtwFeed.WriteEndElement();

即。你必须将这个元素添加到rss xml

<enclosure url="[PictureURL]" type="image/jpeg"></enclosure>