### Write Feed with Custom Namespaces using RssFormatter Source: https://context7.com/dotnet/syndicationfeedreaderwriter/llms.txt This example demonstrates how to use RssFormatter to write a syndication feed with custom namespaces like Media RSS and Dublin Core. It requires importing Microsoft.SyndicationFeed and System.Xml namespaces. ```csharp using Microsoft.SyndicationFeed; using Microsoft.SyndicationFeed.Rss; using System; using System.Collections.Generic; using System.Text; using System.Threading.Tasks; using System.Xml; public class CustomFormatterExample { public static async Task WriteWithCustomNamespaces() { const string MediaNs = "http://search.yahoo.com/mrss/"; const string DcNs = "http://purl.org/dc/elements/1.1/"; var sb = new StringBuilder(); using (XmlWriter xmlWriter = XmlWriter.Create(sb, new XmlWriterSettings() { Async = true, Indent = true })) { // Define custom namespace attributes var attributes = new List() { new SyndicationAttribute("xmlns:media", MediaNs), new SyndicationAttribute("xmlns:dc", DcNs) }; // Create formatter with custom attributes var formatter = new RssFormatter(attributes, xmlWriter.Settings); var writer = new RssFeedWriter(xmlWriter, attributes, formatter); await writer.WriteTitle("Media RSS Feed"); await writer.WriteDescription("Feed with media extensions"); await writer.Write(new SyndicationLink(new Uri("https://example.com/media-feed"))); // Create item with standard properties var item = new SyndicationItem() { Id = "https://example.com/video/1", Title = "Sample Video", Description = "A sample video post", Published = DateTimeOffset.UtcNow }; // Convert to content for custom field additions var content = new SyndicationContent(formatter.CreateContent(item)); // Add Media RSS elements var mediaContent = new SyndicationContent("media:content", MediaNs, null); mediaContent.AddAttribute(new SyndicationAttribute("url", "https://example.com/video.mp4")); mediaContent.AddAttribute(new SyndicationAttribute("type", "video/mp4")); mediaContent.AddAttribute(new SyndicationAttribute("duration", "300")); content.AddField(mediaContent); // Add Dublin Core elements content.AddField(new SyndicationContent("dc:creator", DcNs, "John Doe")); content.AddField(new SyndicationContent("dc:date", DcNs, DateTimeOffset.UtcNow.ToString("O"))); await writer.Write(content); xmlWriter.Flush(); } return sb.ToString(); } } ``` -------------------------------- ### Create an RSS 2.0 Feed with RssFeedWriter Source: https://context7.com/dotnet/syndicationfeedreaderwriter/llms.txt Demonstrates initializing an RssFeedWriter and populating it with channel metadata, links, and multiple feed items. ```csharp using Microsoft.SyndicationFeed; using Microsoft.SyndicationFeed.Rss; using System; using System.Globalization; using System.IO; using System.Text; using System.Threading.Tasks; using System.Xml; public class RssFeedWriterExample { public static async Task CreateRssFeed() { var sb = new StringBuilder(); using (XmlWriter xmlWriter = XmlWriter.Create(sb, new XmlWriterSettings() { Async = true, Indent = true })) { var writer = new RssFeedWriter(xmlWriter); // Write channel metadata await writer.WriteTitle("My RSS Feed"); await writer.WriteDescription("A sample RSS 2.0 feed created with SyndicationFeed"); await writer.WriteLanguage(CultureInfo.GetCultureInfo("en-US")); await writer.WritePubDate(DateTimeOffset.UtcNow); await writer.WriteLastBuildDate(DateTimeOffset.UtcNow); await writer.WriteGenerator("Microsoft.SyndicationFeed.ReaderWriter"); await writer.WriteCopyright("Copyright 2024"); await writer.WriteTimeToLive(TimeSpan.FromMinutes(60)); // Write a link await writer.Write(new SyndicationLink(new Uri("https://example.com/feed"))); // Write managing editor await writer.Write(new SyndicationPerson("editor", "editor@example.com", RssContributorTypes.ManagingEditor)); // Write items for (int i = 1; i <= 3; i++) { var item = new SyndicationItem() { Id = $"https://example.com/posts/{i}", Title = $"Article #{i}", Description = $"This is the content of article {i}.", Published = DateTimeOffset.UtcNow.AddDays(-i) }; item.AddLink(new SyndicationLink(new Uri($"https://example.com/posts/{i}"))); item.AddCategory(new SyndicationCategory("Technology")); item.AddCategory(new SyndicationCategory("News")); item.AddContributor(new SyndicationPerson("Author", "author@example.com")); await writer.Write(item); } xmlWriter.Flush(); } return sb.ToString(); } } ``` -------------------------------- ### Write an RSS item with RssFeedWriter Source: https://github.com/dotnet/syndicationfeedreaderwriter/blob/main/README.md Demonstrates creating an RssFeedWriter and writing a SyndicationItem to an XML stream. Includes a custom StringWriterWithEncoding helper class to specify output encoding. ```cs var sw = new StringWriterWithEncoding(Encoding.UTF8); using (XmlWriter xmlWriter = XmlWriter.Create(sw, new XmlWriterSettings() { Async = true, Indent = true })) { var writer = new RssFeedWriter(xmlWriter); // Create item var item = new SyndicationItem() { Title = "Rss Writer Avaliable", Description = "The new Rss Writer is now available as a NuGet Package!", Id = "https://www.nuget.org/packages/Microsoft.SyndicationFeed.ReaderWriter", Published = DateTimeOffset.UtcNow }; item.AddCategory(new SyndicationCategory("Technology")); item.AddContributor(new SyndicationPerson("test", "test@mail.com")); await writer.Write(item); xmlWriter.Flush(); } class StringWriterWithEncoding : StringWriter { private readonly Encoding _encoding; public StringWriterWithEncoding(Encoding encoding) { this._encoding = encoding; } public override Encoding Encoding { get { return _encoding; } } } ``` -------------------------------- ### Manage Custom Syndication Elements Source: https://context7.com/dotnet/syndicationfeedreaderwriter/llms.txt Demonstrates writing custom elements with attributes and namespaces, and reading them back from an RSS feed. ```csharp using Microsoft.SyndicationFeed; using Microsoft.SyndicationFeed.Rss; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Xml; public class CustomElementExample { // Writing custom elements public static async Task WriteCustomElements(RssFeedWriter writer) { // Create a custom element with attributes and nested fields var customElement = new SyndicationContent("customData"); customElement.AddAttribute(new SyndicationAttribute("version", "1.0")); customElement.AddAttribute(new SyndicationAttribute("enabled", "true")); customElement.AddField(new SyndicationContent("setting", "value1")); customElement.AddField(new SyndicationContent("config", "value2")); await writer.Write(customElement); // Create namespaced custom element const string CustomNs = "http://example.com/custom"; var namespacedElement = new SyndicationContent("metadata", CustomNs, null); namespacedElement.AddField(new SyndicationContent("author", CustomNs, "John Doe")); namespacedElement.AddField(new SyndicationContent("source", CustomNs, "API")); await writer.Write(namespacedElement); } // Reading custom elements from items public static async Task ReadCustomElements(string feedPath) { using (var xmlReader = XmlReader.Create(feedPath, new XmlReaderSettings() { Async = true })) { var parser = new RssParser(); var feedReader = new RssFeedReader(xmlReader, parser); while (await feedReader.Read()) { if (feedReader.ElementType == SyndicationElementType.Item) { // Read as generic content to access custom fields ISyndicationContent content = await feedReader.ReadContent(); // Parse standard item properties ISyndicationItem item = parser.CreateItem(content); Console.WriteLine($"Item: {item.Title}"); // Access custom fields ISyndicationContent customField = content.Fields.FirstOrDefault(f => f.Name == "customData"); if (customField != null) { Console.WriteLine($"Custom Data: {customField.Value}"); foreach (var attr in customField.Attributes) { Console.WriteLine($" Attribute: {attr.Name} = {attr.Value}"); } foreach (var field in customField.Fields) { Console.WriteLine($" Field: {field.Name} = {field.Value}"); } } } } } } } ``` -------------------------------- ### Create SyndicationItem Source: https://context7.com/dotnet/syndicationfeedreaderwriter/llms.txt Instantiate a SyndicationItem with essential properties like ID, title, description, and dates. Supports adding multiple categories, contributors, and links. ```csharp using Microsoft.SyndicationFeed; using System; public class SyndicationItemExample { public static SyndicationItem CreateItem() { var item = new SyndicationItem() { Id = "https://example.com/posts/unique-id", Title = "Understanding Syndication Feeds", Description = "A comprehensive guide to RSS and Atom feeds.", Published = DateTimeOffset.UtcNow, LastUpdated = DateTimeOffset.UtcNow }; // Add multiple categories item.AddCategory(new SyndicationCategory("Technology")); item.AddCategory(new SyndicationCategory("Web Development")); item.AddCategory(new SyndicationCategory("Tutorials") { Scheme = "https://example.com/taxonomy" }); // Add multiple contributors item.AddContributor(new SyndicationPerson("John Doe", "john@example.com") { Uri = "https://example.com/authors/john" }); item.AddContributor(new SyndicationPerson("Jane Smith", "jane@example.com")); // Add multiple links item.AddLink(new SyndicationLink(new Uri("https://example.com/posts/unique-id")) { Title = "Permalink" }); item.AddLink(new SyndicationLink(new Uri("https://example.com/posts/unique-id/comments"), "replies")); item.AddLink(new SyndicationLink(new Uri("https://example.com/posts/unique-id.pdf"), "enclosure") { MediaType = "application/pdf", Length = 102400 }); return item; } } ``` -------------------------------- ### Create RSS Feed with Full Channel Metadata Source: https://context7.com/dotnet/syndicationfeedreaderwriter/llms.txt Demonstrates writing all available RSS channel elements using RssFeedWriter. Ensure all required elements are present before adding optional ones. ```csharp using Microsoft.SyndicationFeed; using Microsoft.SyndicationFeed.Rss; using System; using System.Collections.Generic; using System.Globalization; using System.Text; using System.Threading.Tasks; using System.Xml; public class RssChannelMetadataExample { public static async Task CreateFeedWithFullMetadata() { var sb = new StringBuilder(); using (XmlWriter xmlWriter = XmlWriter.Create(sb, new XmlWriterSettings() { Async = true, Indent = true })) { var writer = new RssFeedWriter(xmlWriter); // Required channel elements await writer.WriteTitle("Complete RSS Feed Example"); await writer.WriteDescription("Demonstrating all RSS channel elements"); await writer.Write(new SyndicationLink(new Uri("https://example.com"))); // Optional channel elements await writer.WriteLanguage(CultureInfo.GetCultureInfo("en-US")); await writer.WriteCopyright("Copyright 2024 Example Inc."); await writer.WritePubDate(DateTimeOffset.UtcNow); await writer.WriteLastBuildDate(DateTimeOffset.UtcNow); await writer.WriteGenerator("SyndicationFeed Library"); await writer.WriteDocs(); // Links to RSS specification // Time to live (cache duration in minutes) await writer.WriteTimeToLive(TimeSpan.FromHours(1)); // Cloud for real-time notifications await writer.WriteCloud( new Uri("https://rpc.example.com:5337/notify"), "myCloud.rssPleaseNotify", "xml-rpc" ); // Skip hours (0-23) when aggregators should not fetch await writer.WriteSkipHours(new byte[] { 0, 1, 2, 3, 4, 5 }); // Skip midnight to 5 AM // Skip days when aggregators should not fetch await writer.WriteSkipDays(new[] { DayOfWeek.Saturday, DayOfWeek.Sunday }); // Channel image var image = new SyndicationImage(new Uri("https://example.com/logo.png")) { Title = "Example Logo", Description = "Our company logo", Link = new SyndicationLink(new Uri("https://example.com")) }; await writer.Write(image); // Category await writer.Write(new SyndicationCategory("Technology") { Scheme = "https://example.com/categories" }); xmlWriter.Flush(); } return sb.ToString(); } } ``` -------------------------------- ### Read an RSS feed with RssFeedReader Source: https://github.com/dotnet/syndicationfeedreaderwriter/blob/main/README.md Uses an XmlReader to initialize an RssFeedReader and iterates through feed elements using a switch statement to handle different syndication types. ```cs using (var xmlReader = XmlReader.Create(filePath, new XmlReaderSettings() { Async = true })) { var feedReader = new RssFeedReader(xmlReader); while(await feedReader.Read()) { switch (feedReader.ElementType) { // Read category case SyndicationElementType.Category: ISyndicationCategory category = await feedReader.ReadCategory(); break; // Read Image case SyndicationElementType.Image: ISyndicationImage image = await feedReader.ReadImage(); break; // Read Item case SyndicationElementType.Item: ISyndicationItem item = await feedReader.ReadItem(); break; // Read link case SyndicationElementType.Link: ISyndicationLink link = await feedReader.ReadLink(); break; // Read Person case SyndicationElementType.Person: ISyndicationPerson person = await feedReader.ReadPerson(); break; // Read content default: ISyndicationContent content = await feedReader.ReadContent(); break; } } } ``` -------------------------------- ### Write Atom Feed with AtomFeedWriter Source: https://context7.com/dotnet/syndicationfeedreaderwriter/llms.txt Use AtomFeedWriter to construct an Atom 1.0 feed, including feed-level metadata, author, self link, and entries. Ensure XmlWriter is configured for asynchronous operations and indentation. ```csharp using Microsoft.SyndicationFeed; using Microsoft.SyndicationFeed.Atom; using System; using System.IO; using System.Text; using System.Threading.Tasks; using System.Xml; public class AtomFeedWriterExample { public static async Task CreateAtomFeed() { var sb = new StringBuilder(); using (XmlWriter xmlWriter = XmlWriter.Create(sb, new XmlWriterSettings() { Async = true, Indent = true })) { var writer = new AtomFeedWriter(xmlWriter); // Write feed metadata await writer.WriteId("urn:uuid:feed-unique-id"); await writer.WriteTitle("My Atom Feed"); await writer.WriteSubtitle("A sample Atom 1.0 feed"); await writer.WriteUpdated(DateTimeOffset.UtcNow); await writer.WriteRights("Copyright 2024 Example Corp"); await writer.WriteGenerator("SyndicationFeed", "https://github.com/dotnet/SyndicationFeedReaderWriter", "1.0.0"); // Write author await writer.Write(new SyndicationPerson("Author Name", "author@example.com")); // Write self link await writer.Write(new SyndicationLink(new Uri("https://example.com/atom.xml"), AtomLinkTypes.Self)); // Write entries var entry = new AtomEntry() { Id = "urn:uuid:entry-1", Title = "First Entry", Description = "Content of the first entry", Summary = "A brief summary", Published = DateTimeOffset.UtcNow, LastUpdated = DateTimeOffset.UtcNow, ContentType = "text", Rights = "Copyright 2024" }; entry.AddLink(new SyndicationLink(new Uri("https://example.com/entry/1"), AtomLinkTypes.Alternate)); entry.AddCategory(new SyndicationCategory("Technology") { Scheme = "https://example.com/categories" }); entry.AddContributor(new SyndicationPerson("Contributor", "contrib@example.com")); await writer.Write(entry); xmlWriter.Flush(); } return sb.ToString(); } } ``` -------------------------------- ### Read Atom Feed Entries and Metadata Source: https://context7.com/dotnet/syndicationfeedreaderwriter/llms.txt Use this method to iterate through an Atom feed, processing various elements like categories, images, entries, links, and persons. It specifically uses ReadEntry to access Atom-specific properties such as Summary, Rights, and ContentType. ```csharp using Microsoft.SyndicationFeed; using Microsoft.SyndicationFeed.Atom; using System; using System.Threading.Tasks; using System.Xml; public class AtomFeedReaderExample { public static async Task ReadAtomFeed(string feedUrl) { using (XmlReader xmlReader = XmlReader.Create(feedUrl, new XmlReaderSettings() { Async = true })) { var reader = new AtomFeedReader(xmlReader); while (await reader.Read()) { switch (reader.ElementType) { case SyndicationElementType.Category: ISyndicationCategory category = await reader.ReadCategory(); Console.WriteLine($"Category: {category.Name}"); break; case SyndicationElementType.Image: ISyndicationImage image = await reader.ReadImage(); Console.WriteLine($"Image: {image.Url}"); break; case SyndicationElementType.Item: // Use ReadEntry for Atom-specific properties IAtomEntry entry = await reader.ReadEntry(); Console.WriteLine($"Entry: {entry.Title}"); Console.WriteLine($" Summary: {entry.Summary}"); Console.WriteLine($" Rights: {entry.Rights}"); Console.WriteLine($" ContentType: {entry.ContentType}"); break; case SyndicationElementType.Link: ISyndicationLink link = await reader.ReadLink(); Console.WriteLine($"Link: {link.Uri} (rel={link.RelationshipType})"); break; case SyndicationElementType.Person: ISyndicationPerson person = await reader.ReadPerson(); Console.WriteLine($"Person: {person.Name}"); break; default: ISyndicationContent content = await reader.ReadContent(); Console.WriteLine($"Content: {content.Name}"); break; } } } } } ``` -------------------------------- ### Read RSS 2.0 Feeds with RssFeedReader Source: https://context7.com/dotnet/syndicationfeedreaderwriter/llms.txt Uses RssFeedReader to process feed elements asynchronously via an XmlReader. Requires handling different SyndicationElementType cases to extract specific feed data. ```csharp using Microsoft.SyndicationFeed; using Microsoft.SyndicationFeed.Rss; using System; using System.Threading.Tasks; using System.Xml; public class RssFeedReaderExample { public static async Task ReadRssFeed(string feedUrl) { using (var xmlReader = XmlReader.Create(feedUrl, new XmlReaderSettings() { Async = true })) { var feedReader = new RssFeedReader(xmlReader); while (await feedReader.Read()) { switch (feedReader.ElementType) { case SyndicationElementType.Category: ISyndicationCategory category = await feedReader.ReadCategory(); Console.WriteLine($"Category: {category.Name}"); break; case SyndicationElementType.Image: ISyndicationImage image = await feedReader.ReadImage(); Console.WriteLine($"Image: {image.Url}"); break; case SyndicationElementType.Item: ISyndicationItem item = await feedReader.ReadItem(); Console.WriteLine($"Item: {item.Title}"); Console.WriteLine($" Published: {item.Published}"); Console.WriteLine($" Description: {item.Description}"); break; case SyndicationElementType.Link: ISyndicationLink link = await feedReader.ReadLink(); Console.WriteLine($"Link: {link.Uri}"); break; case SyndicationElementType.Person: ISyndicationPerson person = await feedReader.ReadPerson(); Console.WriteLine($"Person: {person.Name} ({person.Email})"); break; default: ISyndicationContent content = await feedReader.ReadContent(); Console.WriteLine($"Content: {content.Name} = {content.Value}"); break; } } } } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.