Click here to Skip to main content
15,884,099 members
Articles / Programming Languages / C#
Article

How to Parse the Bookmarks File of Mozilla into a Datatable

Rate me:
Please Sign up or sign in to vote.
4.55/5 (8 votes)
14 Sep 20072 min read 43.9K   805   35   3
An article describing how to parse a Mozilla bookmarks file into a datatable
Screenshot - ImportMozilla1.jpg

Introduction

In this article, I create a procedure to import the file HTML containing the favourites of Mozilla Firefox. It is useful for parsing the file, putting it into a DataTable and then putting it into a TreeView. You can import Mozilla Firefox favourites to your application with this class.

Using the Code

Essentially, the file is an HTML file with particular tags to identify the URLs and folders. When you meet the tag <DT> followed by <H3>, it means that a new folder starts. I used a static method from the class ClsMozilla called ReadBookmark that returns a DataTable, accepting as a parameter the physical name of the file containing the bookmarks. I used StreamReader StreamBookMarks to make a cycle in order to analyze all the rows contained in the file. When I met the relevant tag, I placed the Folder and Links in the DataTable.

C#
int Last_parent_id = 0;
//this is the relation with a folder. 0 means that it is alone
int Before_parent_id = 0;
//when close the folder, this store the prev id

This is done in order to associate each link or folder with a parent folder. This is fundamental when you want to build the tree of the links because you need to know the parent. This is interesting use of the DataTable because, in the first round, I tried to use a multidimensional array, but the management of this last is really more difficult than a DataTable. In the DataTable, I created various columns.

C#
DataTable dt = new DataTable();
dt.Columns.Add( new DataColumn("id", typeof(Int32)));
dt.Columns.Add( new DataColumn("parent_id", typeof(Int32)));
dt.Columns.Add( new DataColumn("Hierarchy", typeof(Int32)));
dt.Columns.Add(new DataColumn("Title", typeof(string)));
dt.Columns.Add(new DataColumn("Link", typeof(string)));
dt.Columns.Add(new DataColumn("Des", typeof(string)));
dt.Columns.Add(new DataColumn("Type", typeof(string)));
DataRow dr ;

In the variable Type, I trace the nature of the link, i.e. if it is a URL or a Folder. I used a simple Regex to remove the HTML code.

C#
public static string StripHTMLTags(string StrText)
{
//On retire le code HTML
return Regex.Replace(StrText,@"<(.|\n)*?>",string.Empty);
 }

Points of Interest

The class is very simple, as is the Firefox file's structure. The favourites of Internet Explorer are simpler because every favourite is a simple file. The Opera bookmark file is similar to Firefox and, in the future, I will release a new article for the other browser.

History

  • 14 September, 2007 -- Original version posted

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer therealbest.com
Italy Italy
I am a professional developer in Milan, I wrote many software and site for firm and bank, I like programming because I think it is an art!
My site is http://www.therealbest.com and http://www.rentvillasin.com

Comments and Discussions

 
QuestionAwesome! Pin
thund3rstruck2-Nov-14 8:31
thund3rstruck2-Nov-14 8:31 
AnswerRe: Awesome! Pin
Pierpaolo Romanelli24-Apr-15 8:59
Pierpaolo Romanelli24-Apr-15 8:59 
GeneralRe: Awesome! Pin
RagTopMan3-Aug-15 12:45
RagTopMan3-Aug-15 12:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.