public static void ExportOpml(string filename, IEnumerable<Feed> feeds, string name) { var opml = new OpmlDocument(); opml.Head.Title = String.Format("{0} Export", name); ; var categories = from f in feeds group f by f.Category into c select new { Name = c.Key, Feeds = c.ToList() }; foreach (var category in categories) { var outline = new OpmlOutline(category.Name); outline.Attributes.Add("title", category.Name); foreach (var feed in category.Feeds) { var let = new OpmlOutline(feed.Title); let.Attributes.Add("type", "rss"); let.Attributes.Add("title", feed.Title); let.Attributes.Add("xmlUrl", feed.Url); let.Attributes.Add("htmlUrl", feed.SiteUrl); outline.Outlines.Add(let); } opml.AddOutline(outline); } using (var stream = File.Open(filename, FileMode.Create, FileAccess.Write, FileShare.None)) { opml.Save(stream); } }
public void ReadSubscriptions(string opmlPath) { using (var stream = new StreamReader(opmlPath)) { _opmlDocument = new OpmlDocument(); _opmlDocument.Load(stream.BaseStream); } }
/// <summary> /// Modifies the <see cref="OpmlDocument"/> to match the data source. /// </summary> /// <param name="resource">The <see cref="OpmlDocument"/> to be filled.</param> /// <exception cref="ArgumentNullException">The <paramref name="resource"/> is a null reference (Nothing in Visual Basic).</exception> public void Fill(OpmlDocument resource) { //------------------------------------------------------------ // Validate parameter //------------------------------------------------------------ Guard.ArgumentNotNull(resource, "resource"); //------------------------------------------------------------ // Create namespace resolver //------------------------------------------------------------ XmlNamespaceManager manager = new XmlNamespaceManager(this.Navigator.NameTable); //------------------------------------------------------------ // Attempt to fill syndication resource //------------------------------------------------------------ XPathNavigator documentNavigator = this.Navigator.SelectSingleNode("opml", manager); if (documentNavigator != null) { XPathNavigator headNavigator = documentNavigator.SelectSingleNode("head", manager); if (headNavigator != null) { resource.Head.Load(headNavigator, this.Settings); } XPathNodeIterator outlineIterator = documentNavigator.Select("body/outline", manager); if (outlineIterator != null && outlineIterator.Count > 0) { int counter = 0; while (outlineIterator.MoveNext()) { OpmlOutline outline = new OpmlOutline(); counter++; if (outline.Load(outlineIterator.Current, this.Settings)) { if (this.Settings.RetrievalLimit != 0 && counter > this.Settings.RetrievalLimit) { break; } ((Collection<OpmlOutline>)resource.Outlines).Add(outline); } } } SyndicationExtensionAdapter adapter = new SyndicationExtensionAdapter(documentNavigator, this.Settings); adapter.Fill(resource, manager); } }
public static IEnumerable<Feed> ImportOpml(string filename) { using (var stream = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.Read)) { var opml = new OpmlDocument(); opml.Load(stream); return from category in opml.Outlines from feed in category.Outlines select new Feed { Title = feed.Attributes["title"], Url = feed.Attributes["xmlUrl"], SiteUrl = feed.Attributes["htmlUrl"], Items = new List<Item>(), Category = category.Attributes["title"], }; } }
//============================================================ // CLASS SUMMARY //============================================================ /// <summary> /// Provides example code for the OpmlOutline class. /// </summary> public static void ClassExample() { #region OpmlOutline OpmlDocument document = new OpmlDocument(); document.Head.Title = "Example OPML List"; document.Head.CreatedOn = new DateTime(2005, 6, 18, 12, 11, 52); document.Head.ModifiedOn = new DateTime(2005, 7, 2, 21, 42, 48); document.Head.Owner = new OpmlOwner("John Doe", "*****@*****.**"); document.Head.VerticalScrollState = 1; document.Head.Window = new OpmlWindow(61, 304, 562, 842); // Create outline that contains child outlines OpmlOutline containerOutline = new OpmlOutline("Feeds"); containerOutline.Outlines.Add(OpmlOutline.CreateSubscriptionListOutline("Argotic", "rss", new Uri("http://www.codeplex.com/Argotic/Project/ProjectRss.aspx"))); containerOutline.Outlines.Add(OpmlOutline.CreateSubscriptionListOutline("Google News", "feed", new Uri("http://news.google.com/?output=atom"))); document.AddOutline(containerOutline); #endregion }
/// <summary> /// Loads the generic syndication feed using the specified <see cref="XPathNavigator"/> and <see cref="SyndicationResourceLoadSettings"/>. /// </summary> /// <param name="navigator">A read-only <see cref="XPathNavigator"/> object for navigating through the syndication resource information.</param> /// <param name="settings">The <see cref="SyndicationResourceLoadSettings"/> object used to configure the load operation of the <see cref="GenericSyndicationFeed"/>.</param> /// <param name="eventData">A <see cref="SyndicationResourceLoadedEventArgs"/> that contains the event data used when raising the <see cref="GenericSyndicationFeed.Loaded"/> event.</param> /// <remarks> /// After the load operation has successfully completed, the <see cref="GenericSyndicationFeed.Loaded"/> event is raised using the specified <paramref name="eventData"/>. /// </remarks> /// <exception cref="ArgumentNullException">The <paramref name="navigator"/> is a null reference (Nothing in Visual Basic).</exception> /// <exception cref="ArgumentNullException">The <paramref name="settings"/> is a null reference (Nothing in Visual Basic).</exception> /// <exception cref="ArgumentNullException">The <paramref name="eventData"/> is a null reference (Nothing in Visual Basic).</exception> /// <exception cref="FormatException">The <paramref name="navigator"/> data does not conform to the expected syndication content format. In this case, the feed remains empty.</exception> private void Load(XPathNavigator navigator, SyndicationResourceLoadSettings settings, SyndicationResourceLoadedEventArgs eventData) { //------------------------------------------------------------ // Validate parameters //------------------------------------------------------------ Guard.ArgumentNotNull(navigator, "navigator"); Guard.ArgumentNotNull(settings, "settings"); Guard.ArgumentNotNull(eventData, "eventData"); //------------------------------------------------------------ // Initialize generic feed based on syndication resource format //------------------------------------------------------------ SyndicationResourceMetadata metadata = new SyndicationResourceMetadata(navigator); if (metadata.Format == SyndicationContentFormat.Atom) { AtomFeed feed = new AtomFeed(); SyndicationResourceAdapter adapter = new SyndicationResourceAdapter(navigator, settings); adapter.Fill(feed, SyndicationContentFormat.Atom); this.Parse(feed); } else if (metadata.Format == SyndicationContentFormat.Rss) { RssFeed feed = new RssFeed(); SyndicationResourceAdapter adapter = new SyndicationResourceAdapter(navigator, settings); adapter.Fill(feed, SyndicationContentFormat.Rss); this.Parse(feed); } else if (metadata.Format == SyndicationContentFormat.Opml) { OpmlDocument opmlDoc = new OpmlDocument(); SyndicationResourceAdapter adapter = new SyndicationResourceAdapter(navigator, settings); adapter.Fill(opmlDoc, SyndicationContentFormat.Opml); this.Parse(opmlDoc); } //------------------------------------------------------------ // Raise Loaded event to notify registered handlers of state change //------------------------------------------------------------ this.OnFeedLoaded(eventData); }
/// <summary> /// Initializes the generic syndication feed using the supplied <see cref="OpmlDocument"/>. /// Since OmplDocument hasn't direct mappings to feeds in this method we simply initialize /// feedFormat to Opml and feedResource to omplDocument /// </summary> /// <param name="feed">The <see cref="RssFeed"/> to build an abstraction against.</param> /// <exception cref="ArgumentNullException">The <paramref name="feed"/> is a null reference (Nothing in Visual Basic).</exception> public void Parse(OpmlDocument opmlDocument) { //------------------------------------------------------------ // Validate parameters //------------------------------------------------------------ Guard.ArgumentNotNull(opmlDocument, "omplDocument"); //------------------------------------------------------------ // Initialize generic feed //------------------------------------------------------------ feedResource = opmlDocument; feedFormat = SyndicationContentFormat.Opml; }
/// <summary> /// Creates a new <see cref="OpmlDocument"/> instance using the specified <see cref="Uri"/>, <see cref="ICredentials"/>, <see cref="IWebProxy"/>, and <see cref="SyndicationResourceLoadSettings"/> object. /// </summary> /// <param name="source">A <see cref="Uri"/> that represents the URL of the syndication resource XML data.</param> /// <param name="options">A <see cref="WebRequestOptions"/> that holds options that should be applied to web requests.</param> /// <param name="settings">The <see cref="SyndicationResourceLoadSettings"/> object used to configure the <see cref="OpmlDocument"/> instance. This value can be <b>null</b>.</param> /// <returns>An <see cref="OpmlDocument"/> object loaded using the <paramref name="source"/> data.</returns> /// <exception cref="ArgumentNullException">The <paramref name="source"/> is a null reference (Nothing in Visual Basic).</exception> /// <exception cref="FormatException">The <paramref name="source"/> data does not conform to the expected syndication content format. In this case, the document remains empty.</exception> public static OpmlDocument Create(Uri source, WebRequestOptions options, SyndicationResourceLoadSettings settings) { //------------------------------------------------------------ // Local members //------------------------------------------------------------ OpmlDocument syndicationResource = new OpmlDocument(); //------------------------------------------------------------ // Validate parameters //------------------------------------------------------------ Guard.ArgumentNotNull(source, "source"); //------------------------------------------------------------ // Create new instance using supplied parameters //------------------------------------------------------------ syndicationResource.Load(source, options, settings); return syndicationResource; }
/// <summary> /// Instantiates a <see cref="ISyndicationResource"/> that conforms to the specified <see cref="SyndicationContentFormat"/> using the supplied <see cref="Stream"/>. /// </summary> /// <param name="stream">The <see cref="Stream"/> used to load the syndication resource.</param> /// <param name="format">A <see cref="SyndicationContentFormat"/> enumeration value that indicates the type syndication resource the <paramref name="stream"/> represents.</param> /// <returns> /// An <see cref="ISyndicationResource"/> object that conforms to the specified <paramref name="format"/>, initialized using the supplied <paramref name="stream"/>. /// If the <paramref name="format"/> is not supported by the provider, returns a <b>null</b> reference. /// </returns> /// <exception cref="ArgumentNullException">The <paramref name="stream"/> is a null reference (Nothing in Visual Basic).</exception> private static ISyndicationResource BuildResource(SyndicationContentFormat format, Stream stream) { //------------------------------------------------------------ // Validate parameters //------------------------------------------------------------ Guard.ArgumentNotNull(stream, "stream"); //------------------------------------------------------------ // Create syndication resource based on content format //------------------------------------------------------------ if (format == SyndicationContentFormat.Apml) { ApmlDocument document = new ApmlDocument(); document.Load(stream); return document; } else if (format == SyndicationContentFormat.Atom) { XPathDocument document = new XPathDocument(stream); XPathNavigator navigator = document.CreateNavigator(); navigator.MoveToRoot(); navigator.MoveToChild(XPathNodeType.Element); if(String.Compare(navigator.LocalName, "entry", StringComparison.OrdinalIgnoreCase) == 0) { AtomEntry entry = new AtomEntry(); entry.Load(navigator); return entry; } else if (String.Compare(navigator.LocalName, "feed", StringComparison.OrdinalIgnoreCase) == 0) { AtomFeed feed = new AtomFeed(); feed.Load(navigator); return feed; } else { return null; } } else if (format == SyndicationContentFormat.BlogML) { BlogMLDocument document = new BlogMLDocument(); document.Load(stream); return document; } else if (format == SyndicationContentFormat.Opml) { OpmlDocument document = new OpmlDocument(); document.Load(stream); return document; } else if (format == SyndicationContentFormat.Rsd) { RsdDocument document = new RsdDocument(); document.Load(stream); return document; } else if (format == SyndicationContentFormat.Rss) { RssFeed feed = new RssFeed(); feed.Load(stream); return feed; } else { return null; } }