public virtual Task WriteCloud(Uri uri, string registerProcedure, string protocol)
        {
            if (uri == null)
            {
                throw new ArgumentNullException(nameof(uri));
            }

            if (!uri.IsAbsoluteUri)
            {
                throw new ArgumentException("Absolute uri required");
            }

            if (string.IsNullOrEmpty(registerProcedure))
            {
                throw new ArgumentNullException(nameof(registerProcedure));
            }

            var cloud = new SyndicationContent(RssElementNames.Cloud);

            cloud.AddAttribute(new SyndicationAttribute("domain", uri.GetComponents(UriComponents.Host, UriFormat.UriEscaped)));
            cloud.AddAttribute(new SyndicationAttribute("port", uri.GetComponents(UriComponents.StrongPort, UriFormat.UriEscaped)));
            cloud.AddAttribute(new SyndicationAttribute("path", uri.GetComponents(UriComponents.PathAndQuery, UriFormat.UriEscaped)));
            cloud.AddAttribute(new SyndicationAttribute("registerProcedure", registerProcedure));
            cloud.AddAttribute(new SyndicationAttribute("protocol", protocol ?? "xml-rpc"));

            return(Write(cloud));
        }
Exemplo n.º 2
0
        private ISyndicationContent GetCategory()
        {
            var logoContent = new SyndicationContent("category", _itunesNs, null);

            logoContent.AddAttribute(new SyndicationAttribute("text", "Kids & Family"));

            return(logoContent);
        }
Exemplo n.º 3
0
        private ISyndicationContent GetLogo()
        {
            var logoContent = new SyndicationContent("image", _itunesNs, null);

            logoContent.AddAttribute(new SyndicationAttribute("href", "https://p.friism.com/podnanza.jpg"));

            return(logoContent);
        }
Exemplo n.º 4
0
        private ISyndicationContent CreateFromContentLink(ISyndicationLink link)
        {
            //
            // content
            var result = new SyndicationContent(AtomElementNames.Content);

            //
            // src
            result.AddAttribute(new SyndicationAttribute(AtomConstants.Source, FormatValue(link.Uri)));

            //
            // type
            if (!string.IsNullOrEmpty(link.MediaType))
            {
                result.AddAttribute(new SyndicationAttribute(AtomConstants.Type, link.MediaType));
            }

            return(result);
        }
Exemplo n.º 5
0
        public virtual Task WriteGenerator(string value, string uri, string version)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            var generator = new SyndicationContent(AtomElementNames.Generator, value);

            if (!string.IsNullOrEmpty(uri))
            {
                generator.AddAttribute(new SyndicationAttribute("uri", uri));
            }

            if (!string.IsNullOrEmpty(version))
            {
                generator.AddAttribute(new SyndicationAttribute("version", version));
            }

            return(Write(generator));
        }
Exemplo n.º 6
0
        private ISyndicationContent CreateFromLink(ISyndicationLink link)
        {
            //
            // link
            var result = new SyndicationContent(AtomElementNames.Link);

            //
            // title
            if (!string.IsNullOrEmpty(link.Title))
            {
                result.AddAttribute(new SyndicationAttribute(AtomElementNames.Title, link.Title));
            }

            //
            // href
            result.AddAttribute(new SyndicationAttribute(AtomConstants.Href, FormatValue(link.Uri)));

            //
            // rel
            if (!string.IsNullOrEmpty(link.RelationshipType))
            {
                result.AddAttribute(new SyndicationAttribute(AtomConstants.Rel, link.RelationshipType));
            }

            //
            // type
            if (!string.IsNullOrEmpty(link.MediaType))
            {
                result.AddAttribute(new SyndicationAttribute(AtomConstants.Type, link.MediaType));
            }

            //
            // length
            if (link.Length > 0)
            {
                result.AddAttribute(new SyndicationAttribute(AtomConstants.Length, FormatValue(link.Length)));
            }

            return(result);
        }
        private static ISyndicationContent ReadSyndicationContent(XmlReader reader)
        {
            var content = new SyndicationContent(reader.LocalName, reader.NamespaceURI, null);

            //
            // Attributes
            if (reader.HasAttributes)
            {
                while (reader.MoveToNextAttribute())
                {
                    ISyndicationAttribute attr = reader.ReadSyndicationAttribute();

                    if (attr != null)
                    {
                        content.AddAttribute(attr);
                    }
                }

                reader.MoveToContent();
            }

            //
            // Content
            if (!reader.IsEmptyElement)
            {
                reader.ReadStartElement();

                //
                // Value
                if (reader.HasValue)
                {
                    content.Value = reader.ReadContentAsString();
                }
                //
                // Children
                else
                {
                    while (reader.IsStartElement())
                    {
                        content.AddField(ReadSyndicationContent(reader));
                    }
                }

                reader.ReadEndElement(); // end
            }
            else
            {
                reader.Skip();
            }

            return(content);
        }
Exemplo n.º 8
0
        public virtual Task WriteText(string name, string value, string type)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException(nameof(name));
            }

            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            var content = new SyndicationContent(name, value);

            if (!string.IsNullOrEmpty(type))
            {
                content.AddAttribute(new SyndicationAttribute(AtomConstants.Type, type));
            }

            return(Write(content));
        }
        public virtual ISyndicationContent CreateContent(ISyndicationCategory category)
        {
            if (category == null)
            {
                throw new ArgumentNullException(nameof(category));
            }

            if (string.IsNullOrEmpty(category.Name))
            {
                throw new FormatException("Invalid category name");
            }

            var content = new SyndicationContent(RssElementNames.Category, category.Name);

            if (category.Scheme != null)
            {
                content.AddAttribute(new SyndicationAttribute(RssConstants.Domain, category.Scheme));
            }

            return(content);
        }
Exemplo n.º 10
0
        private ISyndicationContent CreateSourceContent(ISyndicationLink link)
        {
            var content = new SyndicationContent(link.RelationshipType);

            //
            // Url
            string url = FormatValue(link.Uri);

            if (link.Title != url)
            {
                content.AddAttribute(new SyndicationAttribute(RssElementNames.Url, url));
            }

            //
            // Title
            if (!string.IsNullOrEmpty(link.Title))
            {
                content.Value = link.Title;
            }

            return(content);
        }
Exemplo n.º 11
0
        public virtual ISyndicationContent CreateContent(ISyndicationItem item)
        {
            if (item == null)
            {
                throw new ArgumentNullException(nameof(item));
            }

            // Spec requires to have at least one title or description
            if (string.IsNullOrEmpty(item.Title) && string.IsNullOrEmpty(item.Description))
            {
                throw new ArgumentNullException("RSS Item requires a title or a description");
            }

            // Write <item> tag
            var content = new SyndicationContent(RssElementNames.Item);

            //
            // Title
            if (!string.IsNullOrEmpty(item.Title))
            {
                content.AddField(new SyndicationContent(RssElementNames.Title, item.Title));
            }

            //
            // Links
            ISyndicationLink guidLink = null;

            if (item.Links != null)
            {
                foreach (var link in item.Links)
                {
                    if (link.RelationshipType == RssElementNames.Guid)
                    {
                        guidLink = link;
                    }

                    content.AddField(CreateContent(link));
                }
            }

            //
            // Description
            if (!string.IsNullOrEmpty(item.Description))
            {
                content.AddField(new SyndicationContent(RssElementNames.Description, item.Description));
            }

            //
            // Authors (persons)
            if (item.Contributors != null)
            {
                foreach (var person in item.Contributors)
                {
                    content.AddField(CreateContent(person));
                }
            }

            //
            // Cathegory
            if (item.Categories != null)
            {
                foreach (var category in item.Categories)
                {
                    content.AddField(CreateContent(category));
                }
            }

            //
            // Guid (id)
            if (guidLink == null && !string.IsNullOrEmpty(item.Id))
            {
                var guid = new SyndicationContent(RssElementNames.Guid, item.Id);

                guid.AddAttribute(new SyndicationAttribute(RssConstants.IsPermaLink, "false"));

                content.AddField(guid);
            }

            //
            // PubDate
            if (item.Published != DateTimeOffset.MinValue)
            {
                content.AddField(new SyndicationContent(RssElementNames.PubDate, FormatValue(item.Published)));
            }

            return(content);
        }
Exemplo n.º 12
0
        public virtual ISyndicationContent CreateContent(ISyndicationItem item)
        {
            if (item == null)
            {
                throw new ArgumentNullException(nameof(item));
            }

            if (string.IsNullOrEmpty(item.Id))
            {
                throw new ArgumentNullException("Id");
            }

            if (string.IsNullOrEmpty(item.Title))
            {
                throw new ArgumentNullException("Title");
            }

            if (item.LastUpdated == default(DateTimeOffset))
            {
                throw new ArgumentException("LastUpdated");
            }

            var result = new SyndicationContent(AtomElementNames.Entry);

            //
            // id
            result.AddField(new SyndicationContent(AtomElementNames.Id, item.Id));

            //
            // title
            result.AddField(new SyndicationContent(AtomElementNames.Title, item.Title));

            //
            // updated
            result.AddField(new SyndicationContent(AtomElementNames.Updated, FormatValue(item.LastUpdated)));

            //
            // published
            if (item.Published != default(DateTimeOffset))
            {
                result.AddField(new SyndicationContent(AtomElementNames.Published, FormatValue(item.Published)));
            }

            //
            // link
            bool hasContentLink   = false;
            bool hasAlternateLink = false;

            if (item.Links != null)
            {
                foreach (var link in item.Links)
                {
                    if (link.RelationshipType == AtomLinkTypes.Content)
                    {
                        if (hasContentLink)
                        {
                            throw new ArgumentNullException("Multiple content links are not allowed");
                        }

                        hasContentLink = true;
                    }
                    else if (link.RelationshipType == null || link.RelationshipType == AtomLinkTypes.Alternate)
                    {
                        hasAlternateLink = true;
                    }

                    result.AddField(CreateContent(link));
                }
            }

            //
            // author/contributor
            bool hasAuthor = false;

            if (item.Contributors != null)
            {
                foreach (var c in item.Contributors)
                {
                    if (c.RelationshipType == null || c.RelationshipType == AtomContributorTypes.Author)
                    {
                        hasAuthor = true;
                    }

                    result.AddField(CreateContent(c));
                }
            }

            if (!hasAuthor)
            {
                throw new ArgumentException("Author is required");
            }

            //
            // category
            if (item.Categories != null)
            {
                foreach (var category in item.Categories)
                {
                    result.AddField(CreateContent(category));
                }
            }

            IAtomEntry entry = item as IAtomEntry;

            //
            // content
            if (!string.IsNullOrEmpty(item.Description))
            {
                if (hasContentLink)
                {
                    throw new ArgumentException("Description and content link are not allowed simultaneously");
                }

                var content = new SyndicationContent(AtomElementNames.Content, item.Description);

                //
                // type
                if (entry != null &&
                    !(string.IsNullOrEmpty(entry.ContentType) || entry.ContentType.Equals(AtomConstants.PlainTextContentType, StringComparison.OrdinalIgnoreCase)))
                {
                    content.AddAttribute(new SyndicationAttribute(AtomConstants.Type, entry.ContentType));
                }

                result.AddField(content);
            }
            else
            {
                if (!(hasContentLink || hasAlternateLink))
                {
                    throw new ArgumentException("Description or alternate link is required");
                }
            }

            if (entry != null)
            {
                //
                // summary
                if (!string.IsNullOrEmpty(entry.Summary))
                {
                    result.AddField(new SyndicationContent(AtomElementNames.Summary, entry.Summary));
                }

                //
                // rights
                if (!string.IsNullOrEmpty(entry.Rights))
                {
                    result.AddField(new SyndicationContent(AtomElementNames.Rights, entry.Rights));
                }
            }

            return(result);
        }
Exemplo n.º 13
0
        private static ISyndicationContent ReadSyndicationContent(XmlReader reader)
        {
            string type = null;

            var content = new SyndicationContent(reader.LocalName, reader.NamespaceURI, null);

            //
            // Attributes
            if (reader.HasAttributes)
            {
                while (reader.MoveToNextAttribute())
                {
                    ISyndicationAttribute attr = reader.ReadSyndicationAttribute();

                    if (attr != null)
                    {
                        if (type == null && attr.IsAtom(AtomConstants.Type))
                        {
                            type = attr.Value;
                        }

                        content.AddAttribute(attr);
                    }
                }

                reader.MoveToContent();
            }

            //
            // Content
            if (!reader.IsEmptyElement)
            {
                //
                // Xml (applies to <content>)
                if (XmlUtils.IsXmlMediaType(type) && content.IsAtom(AtomElementNames.Content))
                {
                    if (reader.NodeType != XmlNodeType.Element)
                    {
                        throw new FormatException($"Invalid Xml element");
                    }

                    content.Value = reader.ReadInnerXml();
                }
                else
                {
                    reader.ReadStartElement();

                    //
                    // Xhtml
                    if (XmlUtils.IsXhtmlMediaType(type) && content.IsAtom())
                    {
                        if (reader.NamespaceURI != AtomConstants.XhtmlNamespace)
                        {
                            throw new FormatException($"Invalid Xhtml namespace");
                        }

                        content.Value = reader.ReadInnerXml();
                    }
                    //
                    // Text/Html
                    else if (reader.HasValue)
                    {
                        content.Value = reader.ReadContentAsString();
                    }
                    //
                    // Children
                    else
                    {
                        while (reader.IsStartElement())
                        {
                            content.AddField(ReadSyndicationContent(reader));
                        }
                    }

                    reader.ReadEndElement(); // end
                }
            }
            else
            {
                reader.Skip();
            }

            return(content);
        }
Exemplo n.º 14
0
    public static async Task WriteFeed()
    {
        var sw = new StringWriterWithEncoding(Encoding.UTF8);

        using (XmlWriter xmlWriter = XmlWriter.Create(sw, new XmlWriterSettings()
        {
            Async = true, Indent = true
        }))
        {
            var writer = new RssFeedWriter(xmlWriter);

            //
            // Add Title
            await writer.WriteTitle("Example of RssFeedWriter");

            //
            // Add Description
            await writer.WriteDescription("Hello World, RSS 2.0!");

            //
            // Add Link
            await writer.Write(new SyndicationLink(new Uri("https://github.com/dotnet/SyndicationFeedReaderWriter")));

            //
            // Add managing editor
            await writer.Write(new SyndicationPerson("managingeditor", "*****@*****.**", RssContributorTypes.ManagingEditor));

            //
            // Add publish date
            await writer.WritePubDate(DateTimeOffset.UtcNow);

            //
            // Add custom element
            var customElement = new SyndicationContent("customElement");

            customElement.AddAttribute(new SyndicationAttribute("attr1", "true"));
            customElement.AddField(new SyndicationContent("Company", "Contoso"));

            await writer.Write(customElement);

            //
            // Add Items
            for (int i = 0; i < 5; ++i)
            {
                var item = new SyndicationItem()
                {
                    Id          = "https://www.nuget.org/packages/Microsoft.SyndicationFeed.ReaderWriter",
                    Title       = $"Item #{i + 1}",
                    Description = "The new Microsoft.SyndicationFeed.ReaderWriter is now available as a NuGet package!",
                    Published   = DateTimeOffset.UtcNow
                };

                item.AddLink(new SyndicationLink(new Uri("https://github.com/dotnet/SyndicationFeedReaderWriter")));
                item.AddCategory(new SyndicationCategory("Technology"));
                item.AddContributor(new SyndicationPerson("user", "*****@*****.**"));

                await writer.Write(item);
            }

            //
            // Done
            xmlWriter.Flush();
        }

        //
        // Ouput the feed
        Console.WriteLine(sw.ToString());
    }