Webiant Logo Webiant Logo
  1. No results found.

    Try your search with a different keyword or use * as a wildcard.

RssFeed.cs

using System.Xml;
using System.Xml.Linq;

namespace Nop.Core.Rss;

/// 
/// Represents the RSS feed
/// 
public partial class RssFeed
{
    #region Ctor

    /// 
    /// Initialize new instance of RSS feed
    /// 
    /// Title
    /// Description
    /// Link
    /// Last build date
    public RssFeed(string title, string description, Uri link, DateTimeOffset lastBuildDate)
    {
        Title = new XElement(NopRssDefaults.Title, title);
        Description = new XElement(NopRssDefaults.Description, description);
        Link = new XElement(NopRssDefaults.Link, link);
        LastBuildDate = new XElement(NopRssDefaults.LastBuildDate, lastBuildDate.ToString("r"));
    }

    /// 
    /// Initialize new instance of RSS feed
    /// 
    /// URL
    public RssFeed(Uri link) : this(string.Empty, string.Empty, link, DateTimeOffset.Now)
    {
    }

    #endregion

    #region Properties

    /// 
    /// Attribute extension
    /// 
    public KeyValuePair AttributeExtension { get; set; }

    /// 
    /// Element extensions
    /// 
    public List ElementExtensions { get; } = new List();

    /// 
    /// List of rss items
    /// 
    public List Items { get; set; } = new List();

    /// 
    /// Title
    /// 
    public XElement Title { get; protected set; }

    /// 
    /// Description
    /// 
    public XElement Description { get; protected set; }

    /// 
    /// Link
    /// 
    public XElement Link { get; protected set; }

    /// 
    /// Last build date
    /// 
    public XElement LastBuildDate { get; protected set; }

    #endregion

    #region Methods

    /// 
    /// Load RSS feed from the passed stream
    /// 
    /// Stream
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the asynchronous task whose result contains the RSS feed
    /// 
    public static async Task LoadAsync(Stream stream)
    {
        try
        {
            var document = await XDocument.LoadAsync(stream, LoadOptions.None, default);

            var channel = document.Root?.Element(NopRssDefaults.Channel);

            if (channel == null)
                return null;

            var title = channel.Element(NopRssDefaults.Title)?.Value ?? string.Empty;
            var description = channel.Element(NopRssDefaults.Description)?.Value ?? string.Empty;
            var link = new Uri(channel.Element(NopRssDefaults.Link)?.Value ?? string.Empty);
            var lastBuildDateValue = channel.Element(NopRssDefaults.LastBuildDate)?.Value;
            var lastBuildDate = lastBuildDateValue == null ? DateTimeOffset.Now : DateTimeOffset.ParseExact(lastBuildDateValue, "r", null);

            var feed = new RssFeed(title, description, link, lastBuildDate);

            foreach (var item in channel.Elements(NopRssDefaults.Item))
            {
                feed.Items.Add(new RssItem(item));
            }

            return feed;
        }
        catch
        {
            return null;
        }
    }

    /// 
    /// Get content of this RSS feed
    /// 
    /// Content of RSS feed
    public string GetContent()
    {
        var document = new XDocument();
        var root = new XElement(NopRssDefaults.RSS, new XAttribute("version", "2.0"));
        var channel = new XElement(NopRssDefaults.Channel,
            new XAttribute(XName.Get(AttributeExtension.Key.Name, AttributeExtension.Key.Namespace), AttributeExtension.Value));

        channel.Add(Title, Description, Link, LastBuildDate);

        foreach (var element in ElementExtensions)
        {
            channel.Add(element);
        }

        foreach (var item in Items)
        {
            channel.Add(item.ToXElement());
        }

        root.Add(channel);
        document.Add(root);

        return document.ToString();
    }

    #endregion
}