我是dotNetRDF和SPARQL的新手,我正在尝试从DBPedia中检索一些人员数据。 我已经编写了这个查询并在http://dbpedia.org/sparql的在线编辑器上成功测试了它:
问题是,当我尝试使用下面的代码启动查询时,我收到HTTP异常400,无效请求:
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());
}
任何帮助将不胜感激。在此先感谢;)
答案 0 :(得分:3)
这主要是因为没有正确使用c#verbatim字符串文字。通过使用@"...
,您指示c#不解释\n
之类的转义序列,但要逐字记录。同时,这也允许您编写已包含隐式\n
。
因此,您应该删除查询字符串中的@
或\n
。
另外,我建议始终将()
放在filter
条款旁边而不是以.
结尾:
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");
答案 1 :(得分:2)
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