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

发布于 2024-11-28 11:26:16 字数 2290 浏览 0 评论 0原文

我对 ASP.NET 中的 RSS 提要很陌生,但我很快就掌握了在 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("图片", 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();
            }

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.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 != "")
            {

//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();
            }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清醇 2024-12-05 11:26:16

试试这个:

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

即你必须将此元素添加到 rss xml

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文