I am new to rss feeds in asp.net, but I caught on pretty fast on modifying xml in c#. I want to add a image to the rss2.0. Thanks for any help.
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.WriteElem开发者_JAVA百科entString("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 != "")
{
//WANT TO ADD IMAGE HERE 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();
Edit Note: I now have the right format but the image are not showing up
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();
}
Try this:
xtwFeed.WriteStartElement("enclosure");
xtwFeed.WriteElementString("url", blog.PictureURL);
xtwFeed.WriteElementString("type", image/jpeg);
xtwFeed.WriteEndElement();
i.e. you have to add this element to rss xml
<enclosure url="[PictureURL]" type="image/jpeg"></enclosure>
精彩评论