开发者

How can i add image to Rss in codebehind?

开发者 https://www.devze.com 2023-01-18 00:44 出处:网络
XmlTextWriter xmlRSS = new XmlTextWriter(Response.OutputStream, Encoding.UTF8); Other codes... List<RssItem> rssContents = new List<RssItem>();
XmlTextWriter xmlRSS = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);

Other codes...

List<RssItem> rssContents = new List<RssItem>();

get rssContents data ....

foreach (RssItem item in rssContents)

{

  xmlRSS.WriteStartElement("item");

  xmlRSS.WriteElementString("title", item.Title);

  //How can i add image element here...

  xmlRSS.WriteElementString("description", item.Description);

  xmlRSS.WriteElementString("link", item.Link);

  xmlRSS.WriteElementString("pubDate", item.PublishDate);

  xmlRSS.WriteEndElement();

}

 xmlRSS.WriteEndElement();

 xmlRSS.WriteEndElement();

 xmlRSS.WriteEndDocument();

 x开发者_开发问答mlRSS.Flush();

 xmlRSS.Close();

 Response.End();


xmlRss.WriteElementString("image", "imagedata");

EDIT: OK use the built-in syndication support.

            var feed = new SyndicationFeed();

        var items = new List<SyndicationItem>();

        foreach (var item in rssItems)
        {
            var syndicationItem = new SyndicationItem
            {
                Title = item.Title,
                Content = item.Description,
                PublishDate = item.PublishDate,

            };

            syndicationItem.ElementExtensions.Add(new XElement("image", item.Image));
            syndicationItem.AddPermalink(new Uri(item.Link));

            items.Add(syndicationItem);
        }

        feed.Items = items;

        using (var xml = new XmlTextWriter(Response.OutputStream, Encoding.UTF8))
        {
            feed.SaveAsRss20(xml);
        }
0

精彩评论

暂无评论...
验证码 换一张
取 消