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)

Comments


Comments are closed