我怎样才能保存我的n3文件格式,就像它的原始文件一样

时间:2011-09-26 16:26:13

标签: c# n3 dotnetrdf

我使用c#和dotnetrdflibrery编程我有一个n3文件,我在notpad中打开它并在下面显示

@prefix my: <http://www.codeproject.com/KB/recipes/n3_notation#>.

my:Peter a my:person, my:boy;
    my:suffers my:acrophobia, my:insomnia, my:xenophobia;
    my:name "Peter";
    my:likes my:Kate.

my:Mark a my:person, my:boy;
    my:suffers my:insomnia;
    my:name "Mark".

my:Kate a my:person, my:girl;
    my:name "Kate".

当我用g.savetofile()保存这个文件时,它保存它就像我不喜欢它的这种格式我觉得这个观点不太好看:

@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>.
@prefix xsd: <http://www.w3.org/2001/XMLSchema#>.
@prefix my: <http://www.codeproject.com/KB/recipes/n3_notation#>.

<http://www.codeproject.com/KB/recipes/n3_notation#Kate> <http://www.codeproject.com/KB/recipes/n3_notation#name> "Kate".
<http://www.codeproject.com/KB/recipes/n3_notation#Kate> a <http://www.codeproject.com/KB/recipes/n3_notation#girl>.
<http://www.codeproject.com/KB/recipes/n3_notation#Kate> a <http://www.codeproject.com/KB/recipes/n3_notation#person>.
<http://www.codeproject.com/KB/recipes/n3_notation#Mark> <http://www.codeproject.com/KB/recipes/n3_notation#name> "Mark".
<http://www.codeproject.com/KB/recipes/n3_notation#Mark> <http://www.codeproject.com/KB/recipes/n3_notation#suffers> <http://www.codeproject.com/KB/recipes/n3_notation#insomnia>.
<http://www.codeproject.com/KB/recipes/n3_notation#Mark> a <http://www.codeproject.com/KB/recipes/n3_notation#boy>.
<http://www.codeproject.com/KB/recipes/n3_notation#Mark> a <http://www.codeproject.com/KB/recipes/n3_notation#person>.
<http://www.codeproject.com/KB/recipes/n3_notation#Peter> <http://www.codeproject.com/KB/recipes/n3_notation#likes> <http://www.codeproject.com/KB/recipes/n3_notation#Kate>.
<http://www.codeproject.com/KB/recipes/n3_notation#Peter> <http://www.codeproject.com/KB/recipes/n3_notation#name> "Peter".
<http://www.codeproject.com/KB/recipes/n3_notation#Peter> <http://www.codeproject.com/KB/recipes/n3_notation#suffers> <http://www.codeproject.com/KB/recipes/n3_notation#acrophobia>.
<http://www.codeproject.com/KB/recipes/n3_notation#Peter> <http://www.codeproject.com/KB/recipes/n3_notation#suffers> <http://www.codeproject.com/KB/recipes/n3_notation#insomnia>.
<http://www.codeproject.com/KB/recipes/n3_notation#Peter> <http://www.codeproject.com/KB/recipes/n3_notation#suffers> <http://www.codeproject.com/KB/recipes/n3_notation#xenophobia>.
<http://www.codeproject.com/KB/recipes/n3_notation#Peter> a <http://www.codeproject.com/KB/recipes/n3_notation#boy>.
<http://www.codeproject.com/KB/recipes/n3_notation#Peter> a <http://www.codeproject.com/KB/recipes/n3_notation#person>.
<http://www.dotnetrdf.org/> <http://example.org/createdBy> "Rob Vesse".

这种格式显示所有uri完全符合,我怎么能像第一种格式一样保存它?

请帮帮我

2 个答案:

答案 0 :(得分:0)

    Graph graph1 = new Graph();

    TripleStore store = new TripleStore();
    Notation3Parser n3parser = new Notation3Parser();

    n3parser.Load(graph1, AppPath + "\\n3\\ontology.n3");

    //Create some Nodes
    graph1.NamespaceMap.AddNamespace("my", UriFactory.Create("http://0.0.0.1/#"));
    IUriNode Person = graph1.CreateUriNode("my:firas");
    IUriNode rdfType = graph1.CreateUriNode("my:name");
    IBlankNode dse = graph1.CreateBlankNode("a");
    ILiteralNode robVesse = graph1.CreateLiteralNode("firas");

    Triple t = new Triple(Person, dse, robVesse);
    graph1.Assert(t);

    IUriNode Person1 = graph1.CreateUriNode("my:firas");
    //ILiteralNode LtrNode = graph1.CreateLiteralNode("a", UriFactory.Create(XmlSpecsHelper.XmlSchemaDataTypeString));
    IUriNode rdfType1 = graph1.CreateUriNode("my:a");
    IUriNode robVesse1 = graph1.CreateUriNode("my:person");
    IGraphLiteralNode dfa = graph1.CreateGraphLiteralNode(graph1);

    Triple t1 = new Triple(Person1, rdfType1, robVesse1);
    graph1.Assert(t1);
    IUriNode Person2 = graph1.CreateUriNode("my:firas");
    IUriNode rdfType2 = graph1.CreateUriNode("my:suffers");
    IUriNode robVesse2 = graph1.CreateUriNode("my:insomnia");
    Triple t2 = new Triple(Person2, rdfType2, robVesse2);
    graph1.Assert(t2);

    //SparqlResultSet resultSet = graph.ExecuteQuery(str2) as SparqlResultSet;

    store.Add(graph1);
    Notation3Writer n3w = new Notation3Writer();
    n3w.Save(graph1, AppPath + "\\n3\\ontology.n3");

答案 1 :(得分:0)

    //correction
    String AppPath = HttpContext.Current.Request.PhysicalApplicationPath.ToString();
    Graph graph1 = new Graph();

    TripleStore store = new TripleStore();
    Notation3Parser n3parser = new Notation3Parser();

    n3parser.Load(graph1, AppPath + "\\n3\\ontology.n3");

    //Create some Nodes
    graph1.NamespaceMap.AddNamespace("my", UriFactory.Create("http://0.0.0.1/#"));
    IUriNode Person = graph1.CreateUriNode("my:firas");
    IUriNode rdfType = graph1.CreateUriNode("my:name");
    //IBlankNode dse = graph1.CreateBlankNode("a");
    ILiteralNode robVesse = graph1.CreateLiteralNode("firas");

    Triple t = new Triple(Person, rdfType, robVesse);
    graph1.Assert(t);

    IUriNode Person1 = graph1.CreateUriNode("my:firas");
    //ILiteralNode LtrNode = graph1.CreateLiteralNode("a", UriFactory.Create(XmlSpecsHelper.XmlSchemaDataTypeString));
    IUriNode rdfType1 = graph1.CreateUriNode("my:a");
    IUriNode robVesse1 = graph1.CreateUriNode("my:person");
    IGraphLiteralNode dfa = graph1.CreateGraphLiteralNode(graph1);

    Triple t1 = new Triple(Person1, rdfType1, robVesse1);
    graph1.Assert(t1);
    IUriNode Person2 = graph1.CreateUriNode("my:firas");
    IUriNode rdfType2 = graph1.CreateUriNode("my:suffers");
    IUriNode robVesse2 = graph1.CreateUriNode("my:insomnia");
    Triple t2 = new Triple(Person2, rdfType2, robVesse2);
    graph1.Assert(t2);

    //SparqlResultSet resultSet = graph.ExecuteQuery(str2) as SparqlResultSet;

    store.Add(graph1);
    Notation3Writer n3w = new Notation3Writer();
    n3w.Save(graph1, AppPath + "\\n3\\ontology.n3");