2012-09-11 85 views

回答

4

由於@cygri指出你應該看看Reading RDF文檔。

下面是從Querying with SPARQL網頁,其中顯示加載文件查詢第一個例子:

using System; 
using VDS.RDF; 
using VDS.RDF.Parsing; 
using VDS.RDF.Query; 

public class InMemoryTripleStoreExample 
{ 
    public static void Main(String[] args) 
    { 
     TripleStore store = new TripleStore(); 

     //Load data from a file 
     store.LoadFromFile("example.rdf"); 

     //Execute a raw SPARQL Query 
     //Should get a SparqlResultSet back from a SELECT query 
     Object results = store.ExecuteQuery("SELECT * WHERE {?s ?p ?o}"); 
     if (results is SparqlResultSet) 
     { 
      //Print out the Results 
      SparqlResultSet rset = (SparqlResultSet)results; 
      foreach (SparqlResult result in rset) 
      { 
       Console.WriteLine(result.ToString()); 
      } 
     } 

     //Use the SparqlQueryParser to give us a SparqlQuery object 
     //Should get a Graph back from a CONSTRUCT query 
     SparqlQueryParser sparqlparser = new SparqlQueryParser(); 
     SparqlQuery query = sparqlparser.ParseFromString("CONSTRUCT { ?s ?p ?o } WHERE {?s ?p ?o}"); 
     results = store.ExecuteQuery(query); 
     if (results is IGraph) 
     { 
      //Print out the Results 
      IGraph g = (IGraph)results; 
      foreach (Triple t in g.Triples) 
      { 
       Console.WriteLine(t.ToString()); 
      } 
      Console.WriteLine("Query took " + query.QueryExecutionTime.ToString()); 
     } 
    } 
} 
+0

非常感謝RobV – DafaDil