FOAF parsing and crawling in C#

by Mads Kristensen 24. April 2008 05:48

I’ve been looking deeply into FOAF lately and last week I worked on a parser that could wrap a FOAF document into a strongly typed class for easy consumption by C#. Now I want to share it with anyone interested. It appears there are no FOAF parsers available in C# yet. I wasn’t able to find any.

The parser

The class FoafParser has two public methods, Parse and ParseFriendsAsync, which handles two different approaches in fetching FOAF documents.

The Parse method

This method takes a URL as a parameter which must point to an online FOAF document. It then parses the foaf:Person element which contains fields such as name, birth date, photo, homepage etc. It then looks for people in the FOAF document that are described as someone you know, parses the information available and adds it to a public collection property. The following example parses a FOAF document and binds it to an ASP.NET Repeater.

FoafParser parser = new FoafParser();

parser.Parse(new Uri("http://example.com/foaf.xml"));

 

if (parser.IsParsed)

{

  ltOwner.Text = parser.Owner.Name;

  repFriends.DataSource = parser.Friends;

  repFriends.DataBind();

}

The ParseFriendsAsync method

When you list people you know in a FOAF document, it is common to only provide very little information such as the full name. When that is the case it is also common to provide a link to that persons FOAF document using the rdfs:seeAlso tag. We can then use that link to retrieve all your friend's FOAF documents and parse them as well.

It can take a long time to retrieve all those documents one at a time, but if we do it asynchronously then we can speed things up substantially. When you have called the Parse method on the FoafParser class, you can then call ParseFriendsAsync. It loops through all the friends that the Parse method found in search for the rdfs.seeAlso tag. When it finds it, a web request is made to retrieve the FOAF document of that friend and then parse it for information.

When the ParseFriendsAsync method is finished, it triggers an event you can listen to. All the friends with FOAF documents have now been updated in the collection.

private void RetrieveAsync()

{

  FoafParser parser = new FoafParser();

  parser.Parse(new Uri("http://example.com/foaf.xml"));

 

  if (parser.IsParsed)

  {

    parser.FriendParsingComplete += new EventHandler<EventArgs>(parser_FriendParsingComplete);

    parser.ParseFriendsAsync();

  }

}

 

void parser_FriendParsingComplete(object sender, EventArgs e)

{

  // Now the Friends collection has been updated asynchronously.

  FoafParser parser = (FoafParser)sender;

  Response.Write(parser.Friends.Count);

}

Download

In the zip file below you’ll find the FoafParser.cs class as well as an example .aspx page and code-behind file.

FoafTester.zip (3,77 kb)

* Only $4.95/month ASP.NET & Windows 2008 + IIS 7 Hosting! FREE SQL Included

Tags: ,

The semantic web

Comments

5/7/2008 5:02:52 AM #

Morten Høybye Frederiksen

You should be aware, that while FOAF, as with any other RDF vocabulary, is usually serialized in RDF/XML, it isn't (easily) parsed as plain XML. If you try parsing my FOAF file at http://www.wasab.dk/morten/foaf.rdf, you will find that your parser won't find my friends, and it might not even find me...

If at all possible, use an RDF parser, it will make your life easier. I haven't tried parsing RDF in C# myself, but SemWeb -- http://razor.occams.info/code/semweb/ -- might work for you.

You might want to read Edd's old C# account too: http://times.usefulinc.com/2003/07/25-foafcsharp

Morten Høybye Frederiksen Denmark |

5/7/2008 5:06:09 AM #

Mads Kristensen

I'm aware of the limitation of the parser in that regard. However, using a third-party RDF parser takes the fun out of coding for me. This is also just my first go at the parser. It will evolve over time - without an RDF parser Smile

Mads Kristensen |

5/7/2008 5:37:52 AM #

Morten Høybye Frederiksen

I can certainly appreciate the fun part, so how about making it a real RDF parser over time? Smile

Morten Høybye Frederiksen Denmark |

5/28/2008 4:26:07 AM #

pingback

Pingback from pimp.devwebpro.com

DevWebPro  » Post Topic   » Wrapping A FOAF Document Into A Strongly Typed Class

pimp.devwebpro.com |

1/7/2009 1:11:53 AM #

arachnode.net

A FOAF parser would be a great addition to my project: http://arachnode.net  Interested?

I maintain an open source C# web crawler that uses SQL2005 for data storage and lucene.net for indexing.

Thanks!
-an

arachnode.net United States |

Comments are closed

About the slave

Mads Kristensen Mads Kristensen
Web developer at ZYB and founder of BlogEngine.NET. More...

LinkedIn ZYB Facebook Last.fm Twitter View Mads Kristensen's profile on Technorati

The Lounge

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008