dotNetRDF Http Error when querying DBPedia

594 views Asked by At

I am new to dotNetRDF and SPARQL and I'm trying to retrieve some person data from DBPedia. I have wrote this query and tested it successfully on the online editor at http://dbpedia.org/sparql :

The problem is that when I try to launch the query using the code below, I get an HTTP Exception 400, Invalid Request:

SparqlRemoteEndpoint endpoint = new SparqlRemoteEndpoint(new Uri("http://dbpedia.org/sparql"));

        //Make a SELECT query against the Endpoint
        SparqlResultSet results = endpoint.QueryWithResultSet(@" 
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n
PREFIX type: <http://dbpedia.org/class/yago/>\n
PREFIX prop: <http://dbpedia.org/ontology/>\n
\n
select DISTINCT ?person ?name ?birth ?shortDescription where {\n
?person a dbpedia-owl:Person ;\n
      foaf:name ?name ;\n
      dbpedia-owl:birthDate ?birth ;\n
      dbpprop:shortDescription ?shortDescription .\n
filter langMatches(lang(?name),'en') .\n
filter langMatches(lang(?shortDescription),'en') \n
}\n
LIMIT 10");
        foreach (SparqlResult result in results)
        {
            Console.WriteLine(result.ToString());
        }

Any help would be appreciated. Thanks in advance ;)

2

There are 2 answers

1
RobV On BEST ANSWER

For the XML version error you can instruct dotNetRDF to request results in a non-XML format e.g.

endpoint.ResultsAcceptHeader = "application/sparql-results+json";

Would ask for JSON instead of XML which will avoid the XML version issue.

As the documentation for that property says:

Can be used to workaround buggy endpoints which don't like the broad Accept Header that dotNetRDF sends by default

2
Jörn Hees On

This mainly seems to be an issue with not using the c# verbatim string literal correctly. By using @"... you instruct c# to not interpret escape sequences like \n but take them verbatim. At the same time this also allows you to write multi-line strings which already contain an implicit \n.

So you should either remove the @ or the \n in your query string.

Also i'd recommend to always put () around filter clauses and not end them with a .:

SparqlResultSet results = endpoint.QueryWithResultSet(@" 
    PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
    PREFIX type: <http://dbpedia.org/class/yago/>
    PREFIX prop: <http://dbpedia.org/ontology/>

    select DISTINCT ?person ?name ?birth ?shortDescription where {
          ?person a dbpedia-owl:Person ;
              foaf:name ?name ;
              dbpedia-owl:birthDate ?birth ;
              dbpprop:shortDescription ?shortDescription .
          filter(langMatches(lang(?name),'en'))
          filter(langMatches(lang(?shortDescription),'en'))
    }
    LIMIT 10");