Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 9th, 2012  |  syntax: None  |  size: 1.60 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Quering RDF File in RDFDOTNET
  2. //Define your Graph here - it may be better to use a QueryableGraph if you plan
  3.     //on making lots of Queries against this Graph as that is marginally more performant
  4.     IGraph g = new Graph();
  5.  
  6.     //Load some data into your Graph using the LoadFromFile() extension method
  7.     g.LoadFromFile("C:/Users/admin/Desktop/current/Semantic/test.rdf");
  8.  
  9.     //Use the extension method ExecuteQuery() to make the query against the Graph
  10.     try
  11.     {
  12.  
  13.  
  14.  
  15.  
  16.         String q = " Prefix u:<http://localhost:49682/Semantic/test.rdf> SELECT * WHERE {?x1 u:age ?x2}";
  17.         Object results = g.ExecuteQuery(q);
  18.  
  19.         if (results is SparqlResultSet)
  20.         {
  21.             //SELECT/ASK queries give a SparqlResultSet
  22.             SparqlResultSet rset = (SparqlResultSet)results;
  23.             foreach (SparqlResult r in rset)
  24.             {
  25.                 //Do whatever you want with each Result
  26.             }
  27.         }
  28.         else if (results is IGraph)
  29.         {
  30.             //CONSTRUCT/DESCRIBE queries give a IGraph
  31.             IGraph resGraph = (IGraph)results;
  32.             foreach (Triple t in resGraph.Triples)
  33.             {
  34.                 //Do whatever you want with each Triple
  35.             }
  36.         }
  37.         else
  38.         {
  39.             //If you don't get a SparqlResutlSet or IGraph something went wrong
  40.             //but didn't throw an exception so you should handle it here
  41.             Console.WriteLine("ERROR");
  42.         }
  43.     }
  44.     catch (VDS.RDF.Query.RdfQueryException queryEx)
  45.     {
  46.         //There was an error executing the query so handle it here
  47.         Console.WriteLine(queryEx.Message);
  48.     }