• User Guide
  • API Documentation
  • Developer Guide
  • Support
  • User Guide
  • Advanced SPARQL
  • Result Formatting
Show / Hide Table of Contents
  • Getting Started
    • Core Concepts
    • Hello World
    • Reading RDF
    • Writing RDF
    • Working with Graphs
    • Typed Values and Lists
    • Working with Triple Stores
    • Building SPARQL
    • Querying with SPARQL
    • Updating with SPARQL
    • Formats Supported
    • dotNetRdf Assembly Signing
  • Going Deeper
    • Exceptions
    • Event Model
    • Equality and Comparison
    • Utility Methods
    • Extension Methods
    • Namespace Mapper
    • URI Factory
    • Node Factory
  • Storage API
    • Triple Store Integration
    • Servers API
    • Transactions API
  • Storage Providers
    • Allegro Graph
    • Blazegraph
    • Dataset Files
    • 4store
    • Fuseki
    • In-Memory
    • Sesame
    • SPARQL Query Endpoints
    • SPARQL Query and Update Endpoints
    • SPARQL Graph Store Protocol
    • Stardog
  • Advanced SPARQL
    • Result Formatting
    • SPARQL Datasets
    • Full Text Querying with SPARQL
    • Advanced SPARQL Operations
  • Ontology API
  • Inference and Reasoning
  • Dynamic API
  • Formatting API
  • Configuration API
    • Graphs
    • Triple Stores
    • Object Factories
    • Readers and Writers
    • SPARQL Endpoints (Deprecated)
    • SPARQL Clients
    • Query Processors
    • Update Processors
    • SPARQL Datasets
    • SPARQL Expression Factories
    • SPARQL Operators
    • SPARQL Optimisers
    • Full Text Query
    • Reasoners
    • Storage Providers
    • Static Options
    • Proxy Servers
  • Handlers API
  • JSON-LD API
    • Expansion
    • Compaction
    • Flattening
    • Framing
    • Processor Options
    • Error Handling
  • Linked Data Fragments API
  • RDF Canonicalization API
  • How-To Guides
    • Debug HTTP Communication
    • Debug SPARQL Queries
    • Load OWL
    • Load RDF from a File
    • Load RDF from the Web
    • Minimize Memory Usage
    • Reify Triples
  • Upgrading to dotNetRDF 3.0
    • Key Changes
    • Global Options Changes

Result Formatting

When you make a SPARQL query as detailed on the Querying with SPARQL you typically get back a SparqlResultSet object. While you can trivially dump results to strings using the ToString() method this doesn't give you a particularly pretty output, this page details various methods by which you can format results for display.

Using Formatters

dotNetRDF supports a powerful Formatting API which is discussed in general elsewhere, this can be leveraged for the purposes of formatting results. Since values in each column of a SparqlResult are just normal INode values you can use any of the existing INodeFormatter implementations for this.

For example:


//Assume we already have our query results in the variable results

//Create a formatter
INodeFormatter formatter = new TurtleFormatter();

foreach (SparqlResult result in results)
{
   Console.WriteLine(result.ToString(formatter));
}

The above will print out results using Turtle formatting for the terms, exact formatting will vary depending on the formatter used and arguments passed to the constructor.

Extracting Values as Strings

Sometimes you may prefer to extract the actual values from the nodes as strings e.g.


//Assume we have our results in the variable results

foreach (SparqlResult result in results)
{
  INode n;
  String data;
  if (result.TryGetValue("var", out n))
  {
    switch (n.NodeType)
    {
      case NodeType.Uri:
        data = ((IUriNode)n).Uri.AbsoluteUri;
        break;
      case NodeType.Blank:
        data = ((IBlankNode)n).Label;
        break;
      case NodeType.Literal:
        //You may want to inspect the DataType and Language properties and generate
        //a different string here
        data = ((ILiteralNode)n).Value;
        break;
      default:
        throw new RdfOutputException("Unexpected Node Type");
    }
  }
  else
  {
    data = String.Empty;
  }

  //Do what you want with the extracted string
}
  • Edit this page
In this article
  • Using Formatters
  • Extracting Values as Strings
Back to top Generated by DocFX