Beispiel #1
0
        /// <summary>
        /// Serialize an RSS feed to an XML document.
        /// </summary>
        /// <param name="feed">RSS feed to serialize.</param>
        /// <returns>XML document of the RSS feed.</returns>
        public static XmlDocument Serialize(RssFeed feed)
        {
            var rssDocument = new XmlDocument();
            var xmlNavigator = rssDocument.CreateNavigator();

            using (var writer = xmlNavigator.AppendChild())
            {
                var serializer = new XmlSerializer(typeof(RssFeed));
                var namespaces = new XmlSerializerNamespaces();
                namespaces.Add(string.Empty, string.Empty);
                serializer.Serialize(writer, feed, namespaces);
            }

            return rssDocument;
        }
        /// <summary>
        /// Create an instance of an RSS feed object with the data for this RSS feed.
        /// </summary>
        /// <param name="thisUrl">The URL of the request for this RSS feed.</param>
        /// <returns>Instance of an RSS feed object with the items in the feed.</returns>
        private RssFeed CreateRssFeedObject(string thisUrl)
        {
            var config = CoreWebConfiguration.Configuration.ApplicationLogRssFeed.Channel;

            var feed = new RssFeed
            {
                Channel = new RssChannel
                {
                    Cloud = new RssCloud
                    {
                        Domain = config.Cloud.Domain.Value,
                        Path = config.Cloud.Path.Value,
                        Port = config.Cloud.Port.Value,
                        Protocol = config.Cloud.Protocol.Value,
                        RegisterProcedure = config.Cloud.RegisterProcedure.Value
                    },
                    Copyright = config.Copyright.Value,
                    Description = config.Description.Value,
                    Generator = config.Generator.Value,
                    Image = new RssImage
                    {
                        Description = config.Image.Description.Value,
                        Height = config.Image.Height.Value,
                        Link = config.Image.Link.Value,
                        Title = config.Image.Title.Value,
                        Url = config.Image.Url.Value,
                        Width = config.Image.Width.Value
                    },
                    Language = config.Language.Value,
                    LastBuildDate = DateTime.UtcNow,
                    Link = config.Link.Value,
                    ManagingEditor = config.ManagingEditor.Value,
                    PublishDate = DateTime.UtcNow.Date,
                    Rating = config.Rating.Value,
                    TextInput = new RssTextInput
                    {
                        Description = config.TextInput.Description.Value,
                        Link = config.TextInput.Link.Value,
                        Name = config.TextInput.Name.Value,
                        Title = config.TextInput.Title.Value
                    },
                    TimeToLive = config.TimeToLive.Value,
                    Title = config.Title.Value,
                    WebMaster = config.WebMaster.Value
                }
            };

            var categories = new Collection<RssCategory>();

            foreach (RssCategoryConfigurationElement category in config.Categories)
            {
                categories.Add(new RssCategory
                {
                    Domain = category.Domain,
                    Value = category.Value
                });
            }

            feed.Channel.Categories = categories.ToArray();
            feed.Channel.SkipDays = this.GetDaysOfWeek();
            feed.Channel.SkipHours = this.GetHoursOfDay();
            feed.Channel.Items = this.GetItems(feed.Channel.Title, thisUrl);

            return feed;
        }
        public void TestXmlSerialization()
        {
            var rss = new RssFeed
            {
                Channel = new RssChannel
                {
                    Categories = new[]
                    {
                        new RssCategory
                        {
                            Domain = "http://pelorus.com/",
                            Value = "TestCategory"
                        }
                    },
                    Cloud = new RssCloud
                    {
                        Domain = "http://pelorus.com/",
                        Path = "/subscribe",
                        Port = 80,
                        Protocol = "soap",
                        RegisterProcedure = "register"
                    },
                    Copyright = "(C) Mike Dempster",
                    Description = "Test RSS feed",
                    Generator = "Pelorus",
                    Image = new RssImage
                    {
                        Description = "Test image",
                        Height = 100,
                        Link = "http://pelorus.com/",
                        Title = "Test",
                        Url = "http://pelorus.com/image.png",
                        Width = 100
                    },
                    Items = new[]
                    {
                        new RssItem
                        {
                            Author = "Mike Dempster",
                            Categories = new[]
                            {
                                new RssCategory
                                {
                                    Domain = "http://pelorus.com/",
                                    Value = "Item Category"
                                }
                            }
                        }
                    },
                    Language = "en-us",
                    SkipHours = new[]
                    {
                        RssHourOfDay.Midnight,
                        RssHourOfDay.OneAm
                    },
                    SkipDays = new []
                    {
                        RssDayOfWeek.Sunday,
                        RssDayOfWeek.Wednesday,
                        RssDayOfWeek.Friday
                    }
                }
            };

            var rssDocument = RssSerializer.Serialize(rss);
            File.WriteAllText("C:\\Temp\\PelorusFeed.xml", rssDocument.InnerXml);
            var rssFeed = RssSerializer.Deserialize(rssDocument);

            Assert.IsNotNull(rssFeed);
        }