Notice: Scheduled maintenance is set for Tuesday, Sept 2nd at 1am EST. During this time, we will experience up to 20 minutes of downtime. Thank you for your patience.


I have a SPARQL Query that I am executing through dotNetRDF. The query is given as below:

Object results = store.ExecuteQuery("PREFIX pre:<>" + "SELECT ?name ?Characteristics ?Behaviour " + "WHERE { ?Person pre:hasCharacteristics ?Characteristics. ?Person pre:name ?name. ?Characteristics pre:showsBehaviour ?Behaviour} " + "ORDER BY ?name");

When I display the result in Console using the following:

SparqlResultSet rset = (SparqlResultSet)results; foreach (SparqlResult result in rset) { Console.WriteLine(result.ToString()); }

The result is displayed as follows:

?Characteristics = , ?Behaviour = , ?name = A^^ ?Characteristics = , ?Behaviour = , ?name = A^^

I want to format this output as, say, a tabular format like:

Name | Characteristics | Behaviour A Intutive RightBrained A Linear LeftBrained

or atleast like:

Name = A Characteristics = Intutive Behaviour = RightBrained

Name = A Characteristics = Linear Behaviour = LeftBrained

I went through the link: How can you format SPARQL Query Results in dotnetrdf? and tried working out the solution provided, but the line if (r.HasVariable(var) && r[var] != null) shows an error as there is no HasVariable function for SparqlResult.

So I modified the code, leaving out the r.HasVariable and the following code:

SparqlResultSet rset = (SparqlResultSet)results; foreach (SparqlResult r in rset) { foreach (String var in r.Variables) { if (r[var] != null) { INode n = r[var]; String output; switch (n.NodeType) { case NodeType.Literal: output = ((ILiteralNode)n).Value; Console.Write(output); break;

        case NodeType.Uri:
        output = ((IUriNode)n).ToString();

        output = n.ToString();


The above code gives me the answer as: A A

How can I split only the word Intutive from and also format it in the way mentioned above?

asked 14 Jan '13, 01:21

George%20Abraham's gravatar image

George Abraham
accept rate: 16%

Oops, a typo on my part, HasVariable() should have been HasValue() - will go and correct the other answer

(14 Jan '13, 08:01) Rob Vesse ♦ Rob%20Vesse's gravatar image

@Rob Vesse: Ok...It happens :)

(14 Jan '13, 09:42) George Abraham George%20Abraham's gravatar image

You need to edit the code that deals with URI nodes from the example code you have reproduced, currently you have the following:

case NodeType.Uri:
        output = ((IUriNode)n).ToString();

This is just going to give you the full URI string, what you likely want to do is get the URI and add some actual logic e.g.

case NodeType.URI:
    Uri u = ((IUriNode)n).Uri;
    if (!u.Fragment.Equals(String.Empty))
        //u.Fragment is the fragment ID, the #blah bit of the URI
        //You might need to add .Substring(1) here to avoid including the # 
        //since I can't remember off the top of my head if .Net includes it
        output = u.Fragment;
       //Otherwise use the full URI
       output = u.AbsoluteUri;

If you want to make more complex formatting decisions you need to add more of your own logic.


answered 14 Jan '13, 08:00

Rob%20Vesse's gravatar image

Rob Vesse ♦
accept rate: 29%

@Rob Vesse: Thanks a lot... It worked.. Btw where can I read more on all these as the User Guide that is available on the the website seems not to contain all this stuff?

(14 Jan '13, 09:40) George Abraham George%20Abraham's gravatar image
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported



Asked: 14 Jan '13, 01:21

Seen: 919 times

Last updated: 31 Jan '13, 07:06