gpt4 book ai didi

c# - 如何在网站上进行 SPARQL 查询

转载 作者:行者123 更新时间:2023-11-28 02:14:43 25 4
gpt4 key购买 nike

我从 rdfdotnet wiki 学习的。 SPARQL 如何查询网站?如果我理解正确,我必须先写一个端点比带有 SparqlParameterizedString queryString 的查询字符串或使用字符串。然后,我写

SparqlResultSet results = endpoint.QueryWithResultSet(queryString);

我的代码:

//Define a remote endpoint
//Use the DBPedia SPARQL endpoint with the default Graph set to DBPedia
SparqlRemoteEndpoint endpoint = new SparqlRemoteEndpoint(new Uri("http://localhost:10080"));//, "http://localhost:10080");

//Create a Parameterized String
SparqlParameterizedString queryString = new SparqlParameterizedString();

//Add a namespace declaration
queryString.Namespaces.AddNamespace("lf-maint", new Uri("http://linkedfactory.org/vocab/maintenance#"));
queryString.Namespaces.AddNamespace("foaf", new Uri("http://xmlns.com/foaf/0.1/"));
queryString.Namespaces.AddNamespace("rdfs", new Uri("http://www.w3.org/2000/01/rdf-schema#"));

//Set the SPARQL command
//For more complex queries we can do this in multiple lines by using += on the
//CommandText property
//Note we can use @name style parameters here
queryString.CommandText = "select ?l { ?s a lf-maint:Fault; lf - maint:description ?l filter(lang(?l) = \"de\") }";

SparqlParameterizedString.ExecuteQuery Method (IRdfHandler, ISparqlResultsHandler)

//Make a SELECT query against the Endpoint
//Exception muss noch geschrieben werden VDS.RDF.Query.RdfQueryException
try
{
//retun ist eine HttpWebResponce
endpoint.QueryRaw(queryString);
SparqlResultSet results = endpoint.QueryWithResultSet(queryString);
foreach (SparqlResult result in results)
{
Console.WriteLine(result.ToString());
}

}
//Exception für VDS.RDF.Query.RdfQueryException
catch (RdfQueryException e)
{
Console.WriteLine("Error RdfQueryException: " + e);
}
//Exception für den VDS.RDF.Parsing.RdfParserSelectionException
catch(RdfParserSelectionException e)
{
Console.WriteLine("Error RdfParserSelectionException: " + e); //bekomme fehler beim Parsen
}


//Make a DESCRIBE query against the Endpoint
IGraph g = endpoint.QueryWithResultGraph("DESCRIBE ");
foreach (Triple t in g.Triples)
{
Console.WriteLine(t.ToString());
}

我的图书馆:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VDS.RDF.Parsing;
using VDS.RDF.Query;
using VDS.RDF;

最佳答案

对不起,我仓促表达自己有点不好。重点是我可以对 URI 执行 SPARQL 查询。我想通过 C# 程序来做到这一点。这样我以后就可以将这段代码嵌入到另一个程序中。但我不太确定它是如何工作的。这其中最重要的功能是什么?我的代码是一个好的开始吗?我无法用它进行查询。

关于c# - 如何在网站上进行 SPARQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48541866/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com