public async Task WritePerson()
        {
            var sw = new StringWriterWithEncoding(Encoding.UTF8);

            var p1 = new SyndicationPerson("John Doe", "*****@*****.**");
            var p2 = new SyndicationPerson("Jane Doe", "*****@*****.**", AtomContributorTypes.Contributor)
            {
                Uri = "www.contoso.com/janedoe"
            };

            using (var xmlWriter = XmlWriter.Create(sw))
            {
                var writer = new AtomFeedWriter(xmlWriter);

                await writer.Write(p1);

                await writer.Write(p2);

                await writer.Flush();
            }

            string res = sw.ToString();

            Assert.True(CheckResult(res, $"<author><name>{p1.Name}</name><email>{p1.Email}</email></author><contributor><name>{p2.Name}</name><email>{p2.Email}</email><uri>{p2.Uri}</uri></contributor>"));
        }
Пример #2
0
        public static async Task <string> ToAtom(this IList <Page> pages)
        {
            using (var stringWriter = new StringWriter())
            {
                using (var xmlWriter = XmlWriter.Create(stringWriter,
                                                        new XmlWriterSettings
                {
                    Async = true,
                    Indent = true,
                    Encoding = Encoding.UTF8,
                    WriteEndDocumentOnClose = true
                }))
                {
                    var atomWriter = new AtomFeedWriter(xmlWriter);
                    var tasks      = new List <Task>
                    {
                        atomWriter.WriteTitle(Settings.Title),
                        atomWriter.WriteSubtitle(Settings.Description),
                        atomWriter.WriteId(Settings.Domain),
                        atomWriter.Write(new SyndicationLink(new Uri(Settings.Domain))),
                        atomWriter.WriteUpdated(pages.First().Timestamp.ToLocalTime())
                    };
                    tasks.AddRange(pages.Select(p => atomWriter.Write(p.ToSyndicationItem())));
                    Task.WaitAll(tasks.ToArray());
                    await atomWriter.Flush();
                }

                return(stringWriter.ToString());
            }
        }
        public async Task WriteValue()
        {
            const string   title   = "Example Feed";
            Guid           id      = Guid.NewGuid();
            DateTimeOffset updated = DateTimeOffset.UtcNow.AddDays(-21);

            var sw = new StringWriterWithEncoding(Encoding.UTF8);

            using (var xmlWriter = XmlWriter.Create(sw))
            {
                var writer = new AtomFeedWriter(xmlWriter);

                await writer.WriteTitle(title);

                await writer.WriteId(id.ToString());

                await writer.WriteUpdated(updated);

                await writer.Flush();
            }

            string res = sw.ToString();

            Assert.True(CheckResult(res, $"<title>{title}</title><id>{id}</id><updated>{updated.ToString("r")}</updated>"));
        }
        public async Task WriteEntry()
        {
            var link      = new SyndicationLink(new Uri("https://contoso.com/alternate"));
            var related   = new SyndicationLink(new Uri("https://contoso.com/related"), AtomLinkTypes.Related);
            var self      = new SyndicationLink(new Uri("https://contoso.com/28af09b3"), AtomLinkTypes.Self);
            var enclosure = new SyndicationLink(new Uri("https://contoso.com/podcast"), AtomLinkTypes.Enclosure)
            {
                Title     = "Podcast",
                MediaType = "audio/mpeg",
                Length    = 4123
            };
            var source = new SyndicationLink(new Uri("https://contoso.com/source"), AtomLinkTypes.Source)
            {
                Title       = "Blog",
                LastUpdated = DateTimeOffset.UtcNow.AddDays(-10)
            };
            var author   = new SyndicationPerson("John Doe", "*****@*****.**");
            var category = new SyndicationCategory("Lorem Category");

            //
            // Construct entry
            var entry = new AtomEntry()
            {
                Id          = "https://contoso.com/28af09b3",
                Title       = "Lorem Ipsum",
                Description = "Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit...",
                LastUpdated = DateTimeOffset.UtcNow,
                ContentType = "text/html",
                Summary     = "Proin egestas sem in est feugiat, id laoreet massa dignissim",
                Rights      = $"copyright (c) {DateTimeOffset.UtcNow.Year}"
            };

            entry.AddLink(link);
            entry.AddLink(enclosure);
            entry.AddLink(related);
            entry.AddLink(source);
            entry.AddLink(self);

            entry.AddContributor(author);

            entry.AddCategory(category);

            //
            // Write
            var sw = new StringWriterWithEncoding(Encoding.UTF8);

            using (var xmlWriter = XmlWriter.Create(sw))
            {
                var writer = new AtomFeedWriter(xmlWriter);

                await writer.Write(entry);

                await writer.Flush();
            }

            string res = sw.ToString();

            Assert.True(CheckResult(res, $"<entry><id>{entry.Id}</id><title>{entry.Title}</title><updated>{entry.LastUpdated.ToString("r")}</updated><link href=\"{link.Uri}\" /><link title=\"{enclosure.Title}\" href=\"{enclosure.Uri}\" rel=\"{enclosure.RelationshipType}\" type=\"{enclosure.MediaType}\" length=\"{enclosure.Length}\" /><link href=\"{related.Uri}\" rel=\"{related.RelationshipType}\" /><source><title>{source.Title}</title><link href=\"{source.Uri}\" /><updated>{source.LastUpdated.ToString("r")}</updated></source><link href=\"{self.Uri}\" rel=\"{self.RelationshipType}\" /><author><name>{author.Name}</name><email>{author.Email}</email></author><category term=\"{category.Name}\" /><content type=\"{entry.ContentType}\">{entry.Description}</content><summary>{entry.Summary}</summary><rights>{entry.Rights}</rights></entry>"));
        }
Пример #5
0
        public async Task <string> Render()
        {
            await feed.Flush();

            xml.WriteEndDocument();
            xml.Flush();
            xml.Close();
            return(Encoding.UTF8.GetString(stream.ToArray()));
        }
        public async Task WriteCategory()
        {
            var category = new SyndicationCategory("Test Category");

            var sw = new StringWriterWithEncoding(Encoding.UTF8);

            using (var xmlWriter = XmlWriter.Create(sw))
            {
                var writer = new AtomFeedWriter(xmlWriter);

                await writer.Write(category);

                await writer.Flush();
            }

            string res = sw.ToString();

            Assert.True(CheckResult(res, $"<category term=\"{category.Name}\" />"));
        }
Пример #7
0
        public async Task WriteXmlContent()
        {
            var sw = new StringWriterWithEncoding(Encoding.UTF8);

            string content = "<h1 xmlns=\"boooo\"><b href=\"foo\">Heading</b><br foo=\"bar\" /></h1><br xmlns=\"\" />";

            using (var xmlWriter = XmlWriter.Create(sw))
            {
                var writer = new AtomFeedWriter(xmlWriter);

                await writer.WriteText("content", content, "application/xml");

                await writer.Flush();
            }

            string res = sw.ToString();

            Assert.True(CheckResult(res, $"<content type=\"application/xml\">{content}</content>"));
        }
Пример #8
0
        public async Task WriteXhtmlTextConstruct()
        {
            var sw = new StringWriterWithEncoding(Encoding.UTF8);

            string content = "<h1><b href=\"foo\">Heading</b><br foo=\"bar\" /></h1><br />";

            using (var xmlWriter = XmlWriter.Create(sw))
            {
                var writer = new AtomFeedWriter(xmlWriter);

                await writer.WriteText("title", content, "xhtml");

                await writer.Flush();
            }

            string res = sw.ToString();

            Assert.True(CheckResult(res, $"<title type=\"xhtml\"><div xmlns=\"http://www.w3.org/1999/xhtml\">{content}</div></title>"));
        }
        public async Task WriteContent()
        {
            const string uri       = "https://contoso.com/generator";
            const string version   = "1.0";
            const string generator = "Example Toolkit";

            var sw = new StringWriterWithEncoding(Encoding.UTF8);

            using (var xmlWriter = XmlWriter.Create(sw))
            {
                var writer = new AtomFeedWriter(xmlWriter);

                await writer.WriteGenerator(generator, uri, version);

                await writer.Flush();
            }

            string res = sw.ToString();

            Assert.True(CheckResult(res, $"<generator uri=\"{uri}\" version=\"{version}\">{generator}</generator>"));
        }
Пример #10
0
        public async Task WriteCDATAValue()
        {
            var    sw    = new StringWriterWithEncoding(Encoding.UTF8);
            string title = "Title & Markup";

            using (var xmlWriter = XmlWriter.Create(sw))
            {
                var writer = new AtomFeedWriter(xmlWriter, null, new AtomFormatter()
                {
                    UseCDATA = true
                });

                await writer.WriteTitle(title);

                await writer.Flush();
            }

            var res = sw.ToString();

            Assert.True(CheckResult(res, $"<title><![CDATA[{title}]]></title>"));
        }
        public async Task WriteImage()
        {
            var icon = new SyndicationImage(new Uri("http://contoso.com/icon.ico"), AtomImageTypes.Icon);
            var logo = new SyndicationImage(new Uri("http://contoso.com/logo.png"), AtomImageTypes.Logo);

            var sw = new StringWriterWithEncoding(Encoding.UTF8);

            using (var xmlWriter = XmlWriter.Create(sw))
            {
                var writer = new AtomFeedWriter(xmlWriter);

                await writer.Write(icon);

                await writer.Write(logo);

                await writer.Flush();
            }

            string res = sw.ToString();

            Assert.True(CheckResult(res, $"<icon>{icon.Url}</icon><logo>{logo.Url}</logo>"));
        }
        public async Task WriteLink()
        {
            var sw = new StringWriterWithEncoding(Encoding.UTF8);

            var link = new SyndicationLink(new Uri("http://contoso.com"))
            {
                Title     = "Test title",
                Length    = 123,
                MediaType = "mp3/video"
            };

            using (var xmlWriter = XmlWriter.Create(sw))
            {
                var writer = new AtomFeedWriter(xmlWriter);

                await writer.Write(link);

                await writer.Flush();
            }

            string res = sw.ToString();

            Assert.True(CheckResult(res, $"<link title=\"{link.Title}\" href=\"{link.Uri}\" type=\"{link.MediaType}\" length=\"{link.Length}\" />"));
        }
        public async Task WritePrefixedAtomNs()
        {
            const string title     = "Example Feed";
            const string uri       = "https://contoso.com/generator";
            const string generator = "Example Toolkit";

            var sw = new StringWriterWithEncoding(Encoding.UTF8);

            using (var xmlWriter = XmlWriter.Create(sw))
            {
                var writer = new AtomFeedWriter(xmlWriter,
                                                new ISyndicationAttribute[] { new SyndicationAttribute("xmlns:atom", "http://www.w3.org/2005/Atom") });

                await writer.WriteTitle(title);

                await writer.WriteGenerator(generator, uri, null);

                await writer.Flush();
            }

            string res = sw.ToString();

            Assert.True(CheckResult(res, $"<atom:title>{title}</atom:title><atom:generator uri=\"{uri}\">{generator}</atom:generator>", "atom"));
        }