问题
I have an n3 file formate and i want to delete a node or triple from it how can i do it? should i use sparql query?please help me i want to have an n3 file and want to delete a node from it. i pass a graph that use in my parent form to this delete form and want to work with this graph that i create from an n3 file i mean i read this n3 file and convert it to a graph and send it to this form.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VDS.RDF;
using VDS.RDF.Parsing;
using VDS.RDF.Query;
using System.IO;
using System.Windows;
using System.Runtime.InteropServices;
using VDS.RDF.Writing;
namespace WindowsFormsApplication2
{
public partial class delete : Form
{
Graph gra = new Graph();
public delete(Graph initialValue)
{
InitializeComponent();
ValueFromParent = initialValue;
}
private void delete_Load(object sender, EventArgs e)
{
}
public Graph ValueFromParent
{
set
{
this.gra = value;
}
}
}
}
回答1:
From the documentation on Working with Graphs please see the section titled Asserting and Retracting triples which makes mention of the Assert() and Retract() methods which can be used to do what you've asked.
For example to delete a specific Triple:
//Assuming you already have the triple to delete in a variable t
g.Retract(t);
Or perhaps more usefully deleting all Triples that match a specific Node:
g.Retract(g.GetTriplesWithSubject(g.CreateUriNode(new Uri("http://example.org"))));
If you aren't sure whether a specific Node exists you can do something like the following:
INode n = g.GetUriNode(new Uri("http://example.org"));
//If n is null then the specified Node does not exist in the Graph
if (n != null)
{
g.Retract(g.GetTriplesWithSubject(n));
}
Note that you can't directly delete a Node from the Graph other than by removing all Triples that have it in the Subject/Object position. Also note that this does not remove it from the collection provided by the Nodes property of the Graph currently.
Yes you can also do this via SPARQL but for just removing a few triples that is very much overkill unless you need to remove triples based on some complex criteria which is not easily expressed directly using API selection and retraction methods.
来源:https://stackoverflow.com/questions/7540012/delete-a-node-or-triple-using-dotenetrdf-librery