public void FormatSampleFeedEmpty()
        {
            var feed = new Rss10Feed
            {
                Channel = new Rss10Channel
                {
                    Items = new List <Rss10Item>
                    {
                        new Rss10Item(),
                    },
                },
            };

            var tryFormatResult = Rss10FeedFormatter.TryFormatRss10Feed(feed, out var document);

            Assert.True(tryFormatResult);

            var targetEncoding    = Encoding.UTF8;
            var xmlWriterSettings = new XmlWriterSettings
            {
                Encoding = targetEncoding,
                Indent   = true,
            };

            using (var memoryStream = new MemoryStream())
                using (var streamWriter = new StreamWriter(memoryStream, targetEncoding))
                    using (var xmlWriter = XmlWriter.Create(streamWriter, xmlWriterSettings))
                    {
                        document.WriteTo(xmlWriter);
                        xmlWriter.Flush();

                        var xmlString = targetEncoding.GetString(memoryStream.ToArray());
                        Assert.NotEmpty(xmlString);
                    }
        }
        public void FormatSampleFeed()
        {
            var feed = new Rss10Feed
            {
                Channel = new Rss10Channel
                {
                    Title       = "This is my feed title",
                    Description = "Description of my feed",
                    TextInput   = new Rss10TextInput
                    {
                        Name        = "TextInput",
                        Title       = "My text input",
                        Description = "Description of my text input",
                        Link        = "https://example.org/my-text-input",
                        About       = "https://example.org/my-text-input",
                    },
                    About = "https://example.org/channel",
                    Link  = "https://example.org/channel",
                    Image = new Rss10Image
                    {
                        Title = "My channel image",
                        Link  = "https://example.org/channel",
                        Url   = "https://example.org/image.png",
                        About = "https://example.org/image.png",
                    },
                    Items = new List <Rss10Item>
                    {
                        new Rss10Item
                        {
                            Title       = "My awesome article",
                            Description = "My awesome article description...",
                            Link        = "https://example.org/article",
                            About       = "https://example.org/article",
                        },
                    },
                },
            };

            var tryFormatResult = Rss10FeedFormatter.TryFormatRss10Feed(feed, out var document);

            Assert.True(tryFormatResult);

            var targetEncoding    = Encoding.UTF8;
            var xmlWriterSettings = new XmlWriterSettings
            {
                Encoding = targetEncoding,
                Indent   = true,
            };

            using (var memoryStream = new MemoryStream())
                using (var streamWriter = new StreamWriter(memoryStream, targetEncoding))
                    using (var xmlWriter = XmlWriter.Create(streamWriter, xmlWriterSettings))
                    {
                        document.WriteTo(xmlWriter);
                        xmlWriter.Flush();

                        var xmlString = targetEncoding.GetString(memoryStream.ToArray());
                        Assert.NotEmpty(xmlString);
                    }
        }
Esempio n. 3
0
        public override BaseFeed Parse(string feedXml, XDocument feedDoc)
        {
            var       rdf     = feedDoc.Root;
            var       channel = rdf.GetElement("channel");
            Rss10Feed feed    = new Rss10Feed(feedXml, channel);

            return(feed);
        }
Esempio n. 4
0
        public static bool TryFormatRss10Feed(Rss10Feed feed, out XDocument document, ExtensionManifestDirectory extensionManifestDirectory = null)
        {
            document = default;

            if (feed == null)
            {
                return(false);
            }

            document = new XDocument();

            var rdfElement = new XElement(_rdf + "RDF");

            document.Add(rdfElement);

            var namespaceAliases = new XNamespaceAliasSet();

            namespaceAliases.EnsureNamespaceAlias(Rss10Constants.RdfNamespaceAlias, _rdf);
            namespaceAliases.EnsureNamespaceAlias(alias: null, _rss);

            if (extensionManifestDirectory == null)
            {
                extensionManifestDirectory = ExtensionManifestDirectory.DefaultForRss;
            }

            if (!TryFormatRss10Channel(feed.Channel, namespaceAliases, extensionManifestDirectory, out var channelElement))
            {
                return(false);
            }

            rdfElement.Add(channelElement);

            if (TryFormatRss10Image(feed.Channel.Image, referenceOnly: false, namespaceAliases, extensionManifestDirectory, out var imageElement))
            {
                rdfElement.Add(imageElement);
            }

            if (TryFormatRss10TextInput(feed.Channel.TextInput, referenceOnly: false, namespaceAliases, extensionManifestDirectory, out var textInputElement))
            {
                rdfElement.Add(textInputElement);
            }

            foreach (var itemToFormat in feed.Channel.Items)
            {
                if (TryFormatRss10Item(itemToFormat, referenceOnly: false, namespaceAliases, extensionManifestDirectory, out var itemElement))
                {
                    rdfElement.Add(itemElement);
                }
            }

            foreach (var namespaceAlias in namespaceAliases.OrderBy(x => x.Name.LocalName))
            {
                rdfElement.Add(namespaceAlias);
            }

            return(true);
        }
Esempio n. 5
0
        public static bool TryParseRss10Feed(XDocument document, out Rss10Feed parsedFeed, ExtensionManifestDirectory extensionManifestDirectory = null)
        {
            parsedFeed = default;

            var rdfElement = document?.Element(_rdf + "RDF");

            if (rdfElement == null)
            {
                return(false);
            }

            var        rssNamespace = rdfElement.Attribute("xmlns")?.Value;
            XNamespace rss          = null;

            foreach (var ns in Rss10Constants.RecognizedNamespaces)
            {
                rss = ns;
                if (rssNamespace == ns.NamespaceName)
                {
                    break;
                }
            }

            if (rss == null)
            {
                return(false);
            }

            if (extensionManifestDirectory == null)
            {
                extensionManifestDirectory = ExtensionManifestDirectory.DefaultForRss;
            }

            if (!TryParseRss10Channel(rdfElement.Element(rss + "channel"), rss, extensionManifestDirectory, out var parsedChannel))
            {
                return(false);
            }

            if (TryParseRss10Image(rdfElement.Element(rss + "image"), rss, extensionManifestDirectory, out var parsedImage))
            {
                parsedChannel.Image = parsedImage;
            }

            if (TryParseRss10TextInput(rdfElement.Element(rss + "textinput"), rss, extensionManifestDirectory, out var parsedTextInput))
            {
                parsedChannel.TextInput = parsedTextInput;
            }

            // items
            foreach (var itemElement in rdfElement.Elements(rss + "item"))
            {
                if (TryParseRss10Item(itemElement, rss, extensionManifestDirectory, out var parsedItem))
                {
                    parsedChannel.Items.Add(parsedItem);
                }
            }

            parsedFeed         = new Rss10Feed();
            parsedFeed.Channel = parsedChannel;
            return(true);
        }