Wim De Cleen

Absorbed in software thoughts

<November 2008>
SuMoTuWeThFrSa
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456


Navigation

Events

Belgian Bloggers

Internet Explorer

General.NET

Service-Oriented Architecture (SOA)

Other links

Subscriptions

Post Categories



Microsoft Feeds API, import and export feed lists

Like promised the next episode of the Really Simple Sample. In this episode I will present the next step of importing and exporting feed lists through an xml file. The format can now handle recursive folders and feeds like it is supposed to be. I have coded the FeedImportExport class, an instance of this class can import and export feeds and folders from/to an xml file, it makes use of the data classes FeedData, FeedFolderData and FeedImports.

using System;
using System.Collections.Generic;
using System.Text;

namespace Widec.Feeds{
 
   public class FeedData {
      [System.Xml.Serialization.XmlAttribute("name")]
      public string Name;
      [System.Xml.Serialization.XmlAttribute("url")]
      public string Url;
   }

   public class FeedFolderData {
     [System.Xml.Serialization.XmlAttribute("name")]
     public string Name;
     [System.Xml.Serialization.XmlElement("feed")]
     public List Feeds;
     [System.Xml.Serialization.XmlElement("folder")]
     public List Folders;
   }
 
   [System.Xml.Serialization.XmlRoot("mfi")]
   public class FeedImports {
      [System.Xml.Serialization.XmlElement("folder")]
      public List Folders;
      [System.Xml.Serialization.XmlElement("feed")]
      public List Feeds;

      public static FeedImports Deserialize(string filename) {
         using (System.IO.FileStream stream = new System.IO.FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read)) {
            System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(FeedImports));
            return (FeedImports)serializer.Deserialize(stream);
         }
      }

      public void Serialize(string filename) {
         using (System.IO.FileStream stream = new System.IO.FileStream(filename,
                         System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite)) {
            System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(FeedImports));
            serializer.Serialize(stream,this);
         } 
      }
   }
}

The FeedImportExport class is a basic recursive import and export reader/writer. You use the Import and Export method to do your work, these methods call the recursive ImportFeed/ExportFeed and ImportFeedFolder/ExportFeedFolder methods. The Export method has one additional feature, it splits the pathname of the FeedFolder and generates the needed folder elements in the xml.

using System;
using System.Collections.Generic;
using System.Text;
using Feeds;

namespace Widec.Feeds {
   public class FeedImportExport{
      public void Import(FeedImports import) {
         IFeedsManager manager = new FeedsManagerClass();
         IFeedFolder rootFolder = (IFeedFolder)manager.RootFolder;

         if (import.Folders != null) {
            foreach (FeedFolderData folderData in import.Folders) {
               ImportFeedFolder(folderData, rootFolder);
            }
         }

         if (import.Feeds != null) {
            foreach (FeedData feedData in import.Feeds) {
               ImportFeed(feedData, rootFolder);
            }
         }
      }

      public FeedImports Export(string folderPath) {
         FeedImports import = new FeedImports();
         import.Folders = new List();
         IFeedsManager manager = new FeedsManagerClass();
         IFeedFolder exportFolder = (IFeedFolder)manager.GetFolder(folderPath);


         string[] Folders = folderPath.Split('\\');
         FeedFolderData data = null;
   
         foreach (string folderName in Folders) {
            FeedFolderData folderData = new FeedFolderData();
            folderData.Name = folderName;
            if (data == null){
               import.Folders.Add(folderData);
            }else{
               data.Folders = new List();
               data.Folders.Add(folderData);  
            }
            data = folderData;
         }

         foreach (IFeed feed in (IFeedsEnum)exportFolder.Feeds) {
            ExportFeed(feed, data);
         }

         foreach (IFeedFolder folder in (IFeedsEnum)exportFolder.Subfolders) {
            ExportFeedFolder(folder, data);
         }

         return import;  
      }

      private void ExportFeed(IFeed feed, FeedFolderData folderData) {
         if (folderData.Feeds == null) {
            folderData.Feeds = new List();
         }
         FeedData data = new FeedData();
         data.Name = feed.name;
         data.Url = feed.url;
         folderData.Feeds.Add(data);
      }

      private void ExportFeedFolder(IFeedFolder folder, FeedFolderData folderData) {
         if (folderData.Folders == null) {
            folderData.Folders = new List();
         }

         FeedFolderData current = new FeedFolderData();
         current.Name = folder.name;
         folderData.Folders.Add(current);
   
         foreach (IFeedFolder subFolder in (IFeedsEnum)folder.Subfolders) {
            ExportFeedFolder(subFolder, current);
         }
   
         foreach (IFeed feed in (IFeedsEnum)folder.Feeds) {
            ExportFeed(feed, current);
         }
      }


      private void ImportFeed(FeedData data, IFeedFolder folder){
         if (!folder.ExistsFeed(data.Name)) {
            folder.CreateFeed(data.Name, data.Url);
         }
      }

      private void ImportFeedFolder(FeedFolderData data, IFeedFolder folder){
         IFeedFolder importFolder;
         if (folder.ExistsSubfolder(data.Name)) {
            importFolder = (IFeedFolder)folder.GetSubfolder(data.Name);
         } else {
            importFolder = (IFeedFolder)folder.CreateSubfolder(data.Name);
         }

         if (data.Feeds != null) {
            foreach (FeedData feedData in data.Feeds) {
               ImportFeed(feedData, importFolder);
            } 
         }

         if (data.Folders != null) {
            foreach (FeedFolderData folderData in data.Folders) {
               ImportFeedFolder(folderData, importFolder);
            }  
         }   
      }
   }
}

Really simple, but a very handy class. Next time I will create a form to view, import and export the Common Feed List and show how to use the FeedEnclosure.AsyncDownload and FeedEnclosure.CancelAsyncDownload, so stay tuned. At that time I will include the complete sample code.

 

posted on Wednesday, February 22, 2006 7:11 PM by widec


# re: Microsoft Feeds API, import and export feed lists @ Wednesday, August 01, 2007 10:52 AM

Nice...

Milos

# re: Microsoft Feeds API, import and export feed lists @ Thursday, August 02, 2007 6:10 PM

interesting

Kostantinos

# re: Microsoft Feeds API, import and export feed lists @ Friday, August 03, 2007 9:56 AM

Cool...

Haralambos

# re: Microsoft Feeds API, import and export feed lists @ Friday, August 03, 2007 5:18 PM

Nice!

Kosmas

# re: Microsoft Feeds API, import and export feed lists @ Saturday, August 04, 2007 6:34 AM

Nice...

Metrophanes

# re: Microsoft Feeds API, import and export feed lists @ Saturday, August 04, 2007 11:55 AM

Cool...

Lazaros

# re: Microsoft Feeds API, import and export feed lists @ Saturday, August 04, 2007 1:42 PM

Nice!

Tzannas

# re: Microsoft Feeds API, import and export feed lists @ Sunday, August 05, 2007 1:03 PM

Cool...

Zacharias

# re: Microsoft Feeds API, import and export feed lists @ Sunday, August 05, 2007 4:42 PM

Sorry :(

Hermes

# re: Microsoft Feeds API, import and export feed lists @ Sunday, August 05, 2007 6:09 PM

Nice!

Thrasyvoulos

# re: Microsoft Feeds API, import and export feed lists @ Monday, August 06, 2007 3:10 AM

Nice!

Haralambos

# re: Microsoft Feeds API, import and export feed lists @ Monday, August 06, 2007 4:05 AM

Sorry :(

Ambrosios

# re: Microsoft Feeds API, import and export feed lists @ Monday, August 06, 2007 4:12 PM

Interesting...

Vangelis

# re: Microsoft Feeds API, import and export feed lists @ Monday, August 06, 2007 8:51 PM

Cool...

Doxiadis

# re: Microsoft Feeds API, import and export feed lists @ Tuesday, August 07, 2007 11:40 AM

Cool!

Yanni

# re: Microsoft Feeds API, import and export feed lists @ Tuesday, August 07, 2007 1:04 PM

Nice!

Dimitri

# re: Microsoft Feeds API, import and export feed lists @ Tuesday, August 07, 2007 7:22 PM

Nice!

Aiakos

# re: Microsoft Feeds API, import and export feed lists @ Wednesday, August 08, 2007 5:06 PM

interesting

Demetris

# re: Microsoft Feeds API, import and export feed lists @ Thursday, August 09, 2007 8:33 AM

Cool!

Ignatios

# re: Microsoft Feeds API, import and export feed lists @ Thursday, August 09, 2007 10:33 PM

Interesting...

Kymon

# re: Microsoft Feeds API, import and export feed lists @ Saturday, August 11, 2007 12:00 AM

interesting

Zenon

# re: Microsoft Feeds API, import and export feed lists @ Saturday, August 11, 2007 7:26 AM

Nice...

Emmanouil

# re: Microsoft Feeds API, import and export feed lists @ Saturday, August 11, 2007 4:19 PM

Nice...

Lefteris

# re: Microsoft Feeds API, import and export feed lists @ Saturday, August 11, 2007 4:39 PM

Nice...

Euaggelos

# re: Microsoft Feeds API, import and export feed lists @ Saturday, August 11, 2007 11:26 PM

Nice

Stephanos

# re: Microsoft Feeds API, import and export feed lists @ Sunday, August 12, 2007 12:06 AM

Nice!

Orion

# re: Microsoft Feeds API, import and export feed lists @ Sunday, August 12, 2007 8:30 AM

Cool...

Gustas

# re: Microsoft Feeds API, import and export feed lists @ Sunday, August 12, 2007 9:25 AM

Nice

Ignatios

# re: Microsoft Feeds API, import and export feed lists @ Sunday, August 12, 2007 4:26 PM

Cool...

Isaakios

# re: Microsoft Feeds API, import and export feed lists @ Sunday, August 12, 2007 9:43 PM

Nice

Skyros

# re: Microsoft Feeds API, import and export feed lists @ Sunday, August 12, 2007 10:52 PM

Nice

Pavlos

# re: Microsoft Feeds API, import and export feed lists @ Sunday, August 12, 2007 10:58 PM

Cool.

Prokopios

# re: Microsoft Feeds API, import and export feed lists @ Sunday, August 12, 2007 11:47 PM

Cool.

Aiolos

# re: Microsoft Feeds API, import and export feed lists @ Monday, August 13, 2007 5:13 AM

Cool!

Panayiotis

# re: Microsoft Feeds API, import and export feed lists @ Tuesday, August 14, 2007 6:32 PM

Cool...

Vassilis

# re: Microsoft Feeds API, import and export feed lists @ Tuesday, August 14, 2007 6:32 PM

Nice

Kyriakos

# re: Microsoft Feeds API, import and export feed lists @ Wednesday, August 15, 2007 3:39 AM

Sorry :(

Evagelos

# re: Microsoft Feeds API, import and export feed lists @ Wednesday, August 15, 2007 6:50 AM

Cool!

Hristos

# re: Microsoft Feeds API, import and export feed lists @ Wednesday, August 15, 2007 7:48 AM

interesting

Dimitrios

# re: Microsoft Feeds API, import and export feed lists @ Wednesday, August 15, 2007 9:47 AM

Cool...

Cletus

# re: Microsoft Feeds API, import and export feed lists @ Wednesday, August 15, 2007 2:55 PM

Cool!

Anastasios

# re: Microsoft Feeds API, import and export feed lists @ Wednesday, August 15, 2007 4:58 PM

Cool!

Koinos

# re: Microsoft Feeds API, import and export feed lists @ Wednesday, August 15, 2007 8:21 PM

Interesting...

Achilleas

# re: Microsoft Feeds API, import and export feed lists @ Wednesday, August 15, 2007 8:40 PM

Nice!

Doxiadis

# re: Microsoft Feeds API, import and export feed lists @ Thursday, August 16, 2007 2:04 AM

Nice

Kris

# re: Microsoft Feeds API, import and export feed lists @ Thursday, August 16, 2007 10:52 AM

Interesting...

Panagiotis

# re: Microsoft Feeds API, import and export feed lists @ Thursday, August 16, 2007 10:55 AM

Cool.

Yannas

# re: Microsoft Feeds API, import and export feed lists @ Thursday, August 16, 2007 8:49 PM

Cool...

Haralambos

# re: Microsoft Feeds API, import and export feed lists @ Friday, August 17, 2007 4:58 PM

Sorry :(

Zenon

# re: Microsoft Feeds API, import and export feed lists @ Friday, August 17, 2007 8:27 PM

Cool.

Epaminondas

# re: Microsoft Feeds API, import and export feed lists @ Saturday, August 18, 2007 8:45 AM

Cool.

Dionyssios

# re: Microsoft Feeds API, import and export feed lists @ Saturday, August 18, 2007 3:55 PM

Nice

Tataki

# re: Microsoft Feeds API, import and export feed lists @ Saturday, August 18, 2007 10:55 PM

Cool!

Aristides

# re: Microsoft Feeds API, import and export feed lists @ Sunday, August 19, 2007 2:00 AM

Nice!

Vaggelis

# re: Microsoft Feeds API, import and export feed lists @ Sunday, August 19, 2007 5:23 AM

Nice!

Sterghios

# re: Microsoft Feeds API, import and export feed lists @ Sunday, August 19, 2007 6:51 AM

Cool!

Titos

# re: Microsoft Feeds API, import and export feed lists @ Sunday, August 19, 2007 12:27 PM

Nice

Panos

# re: Microsoft Feeds API, import and export feed lists @ Sunday, August 19, 2007 2:08 PM

interesting

Leandros

# re: Microsoft Feeds API, import and export feed lists @ Sunday, August 19, 2007 2:42 PM

Nice...

Ilias

# re: Microsoft Feeds API, import and export feed lists @ Sunday, August 19, 2007 7:37 PM

Nice!

Fanos

# re: Microsoft Feeds API, import and export feed lists @ Sunday, August 19, 2007 11:06 PM

Cool.

Yiorgos

# re: Microsoft Feeds API, import and export feed lists @ Monday, August 20, 2007 1:53 AM

Cool.

Athan

# re: Microsoft Feeds API, import and export feed lists @ Monday, August 20, 2007 5:39 AM

Sorry :(

Argyros

# re: Microsoft Feeds API, import and export feed lists @ Monday, August 20, 2007 6:39 AM

Nice

Vasileios

# re: Microsoft Feeds API, import and export feed lists @ Monday, August 20, 2007 7:59 PM

Cool...

Halu

# re: Microsoft Feeds API, import and export feed lists @ Monday, August 20, 2007 8:26 PM

Nice!

Constandinos

# re: Microsoft Feeds API, import and export feed lists @ Monday, August 20, 2007 10:14 PM

Cool...

Cletus

# re: Microsoft Feeds API, import and export feed lists @ Monday, August 20, 2007 10:43 PM

Cool.

Theodosios

# re: Microsoft Feeds API, import and export feed lists @ Tuesday, August 21, 2007 2:30 AM

Cool...

Michalis

# re: Microsoft Feeds API, import and export feed lists @ Tuesday, August 21, 2007 9:57 AM

Cool!

Valerios

# re: Microsoft Feeds API, import and export feed lists @ Tuesday, August 21, 2007 3:26 PM

Cool.

Tassos

# re: Microsoft Feeds API, import and export feed lists @ Tuesday, August 21, 2007 3:54 PM

Cool.

Gustas

# re: Microsoft Feeds API, import and export feed lists @ Wednesday, August 22, 2007 2:12 AM

Nice

Yiorgos

# re: Microsoft Feeds API, import and export feed lists @ Wednesday, August 22, 2007 6:19 AM

Sorry :(

Gerasimos

# re: Microsoft Feeds API, import and export feed lists @ Wednesday, August 22, 2007 1:20 PM

Nice!

Costa

# re: Microsoft Feeds API, import and export feed lists @ Wednesday, August 22, 2007 3:24 PM

Nice...

Giorgos

# re: Microsoft Feeds API, import and export feed lists @ Wednesday, August 22, 2007 5:42 PM

Sorry :(

Makarios

# re: Microsoft Feeds API, import and export feed lists @ Wednesday, August 22, 2007 8:59 PM

Cool.

Alexis

# re: Microsoft Feeds API, import and export feed lists @ Wednesday, August 22, 2007 9:13 PM

Cool!

Andreas

# re: Microsoft Feeds API, import and export feed lists @ Thursday, August 23, 2007 11:43 AM

Sorry :(

Panos

# re: Microsoft Feeds API, import and export feed lists @ Thursday, August 23, 2007 12:07 PM

interesting

Angelos

# re: Microsoft Feeds API, import and export feed lists @ Thursday, August 23, 2007 7:03 PM

Nice!

Stathis

# re: Microsoft Feeds API, import and export feed lists @ Thursday, August 23, 2007 8:46 PM

Sorry :(

Gerasimos

# re: Microsoft Feeds API, import and export feed lists @ Thursday, August 23, 2007 9:38 PM

Cool.

Ivan

# re: Microsoft Feeds API, import and export feed lists @ Thursday, August 23, 2007 10:23 PM

Interesting...

Orion

# re: Microsoft Feeds API, import and export feed lists @ Friday, August 24, 2007 5:55 AM

Cool.

Anninos

# re: Microsoft Feeds API, import and export feed lists @ Friday, August 24, 2007 6:21 AM

Cool.

Euaggelos

# re: Microsoft Feeds API, import and export feed lists @ Friday, August 24, 2007 7:09 AM

Cool!

Leontios

# re: Microsoft Feeds API, import and export feed lists @ Friday, August 24, 2007 1:16 PM

Nice...

Elias

# re: Microsoft Feeds API, import and export feed lists @ Friday, August 24, 2007 1:23 PM

Cool...

Stratos

# re: Microsoft Feeds API, import and export feed lists @ Friday, August 24, 2007 9:32 PM

Cool.

Spiro

# re: Microsoft Feeds API, import and export feed lists @ Saturday, August 25, 2007 12:00 AM

Nice...

Themestoclis

# re: Microsoft Feeds API, import and export feed lists @ Saturday, August 25, 2007 12:59 AM

Cool!

Sotiris

# re: Microsoft Feeds API, import and export feed lists @ Saturday, August 25, 2007 6:14 AM

Nice

Costa

# re: Microsoft Feeds API, import and export feed lists @ Saturday, August 25, 2007 6:53 AM

Cool.