2017-06-17 19 views
0

オントロジーのオブジェクトをリストに保存する際に問題があります。私は簡単な例をxml(owlファイルではない)でしたが、今私はどのように単一の学生のためにこれらの値を得ることができるのか分かりません。OWLファイルから解析してリストに保存する

オントロジーフラグメント:

<Ontology /> 
. 
. //here are 250 lines of declarations etc 
. 
    <ClassAssertion> 
    <Class IRI="#Stacjonarny" /> 
    <NamedIndividual IRI="#Student_3" /> 
    </ClassAssertion> 
    <DataPropertyAssertion> 
    <DataProperty IRI="#student_id" /> 
    <NamedIndividual IRI="#Student_3" /> 
    <Literal datatypeIRI="http://www.w3.org/2001/XMLSchema#integer">3</Literal> 
    </DataPropertyAssertion> 
    <DataPropertyAssertion> 
    <DataProperty IRI="#imie" /> 
    <NamedIndividual IRI="#Student_3" /> 
    <Literal datatypeIRI="http://www.w3.org/2001/XMLSchema#string">Grzegorz</Literal> 
    </DataPropertyAssertion> 
    <DataPropertyAssertion> 
    <DataProperty IRI="#nazwisko" /> 
    <NamedIndividual IRI="#Student_3" /> 
    <Literal datatypeIRI="http://www.w3.org/2001/XMLSchema#string">Brzęczyszczykiewicz</Literal> 
    </DataPropertyAssertion> 
    <DataPropertyAssertion> 
    <DataProperty IRI="#wydział" /> 
    <NamedIndividual IRI="#Student_3" /> 
    <Literal datatypeIRI="http://www.w3.org/2001/XMLSchema#string">Matematyczno-przyrodniczy</Literal> 
    </DataPropertyAssertion> 
</Ontology> 

私は値を取りたい「リテラル」マーカーを形成し、student_3、4のためのリストにそれらを保存し、学生など5など。

学生:

class Student 
{ 
    public int student_id { get; set; } 
    public string imie { get; set; } 
    public string nazwisko { get; set; } 
    public string wydzial { get; set; } 

    public Student(string imie, string nazwisko, string wydzial, int student_id) 
    { 
     this.student_id = student_id; 
     this.imie = imie; 
     this.nazwisko = nazwisko; 
     this.wydzial = wydzial; 
    } 
} 

昨日私は今、私はこのようなものでね、リハーサルではほぼ半日を過ごした:

class Pobierz 
{ 
    List<classes.Student> studenci = new List<classes.Student>(); 


    public void studentow() 
    { 

     XDocument xml = XDocument.Load("moja_ontologia.owl"); 

     studenci = from student in xml.Root.Descendants("DataPropertyAssertion") 
        where student.Attribute("NamedIndividual").Value.Equals("Student") 
        select new classes.Student 
        { 
         imie = student.Element("") 
        } 
    } 
} 

I was based on this

を1 sententionで:これらの値を「リテラル」からどのように得ることができますか?あなたがシリアライズ&逆シリアル化を使用する必要があります

+0

このコードはコンパイルされません。 Studentクラスの空のコンストラクタがなく、リストとして宣言された変数にLINQクエリを割り当てることはできません。 –

+0

あなたはhttps://github.com/bpellens/owldotnetapiを見ましたか? –

+0

@Sir Rufo はい、ありましたが私は自分のパーサーをする必要があります、それは大学のための小さなプロジェクトです。さて、私は空のコンストラクタがあり、List(単にvar studenci)として宣言された変数にクエリを割り当てません。 – Kliwer

答えて

0

....

宣言し、このクラス(あなたは一例で、このクラスを再構築したい場合は、XMLの構造変化は、ダミアン・Drygiel @の答えをhereを見れば):

/// <remarks/> 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] 
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)] 
public partial class Ontology 
{ 

    private OntologyClassAssertion classAssertionField; 

    private OntologyDataPropertyAssertion[] dataPropertyAssertionField; 

    /// <remarks/> 
    public OntologyClassAssertion ClassAssertion 
    { 
     get 
     { 
      return this.classAssertionField; 
     } 
     set 
     { 
      this.classAssertionField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlElementAttribute("DataPropertyAssertion")] 
    public OntologyDataPropertyAssertion[] DataPropertyAssertion 
    { 
     get 
     { 
      return this.dataPropertyAssertionField; 
     } 
     set 
     { 
      this.dataPropertyAssertionField = value; 
     } 
    } 
} 

/// <remarks/> 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] 
public partial class OntologyClassAssertion 
{ 

    private OntologyClassAssertionClass classField; 

    private OntologyClassAssertionNamedIndividual namedIndividualField; 

    /// <remarks/> 
    public OntologyClassAssertionClass Class 
    { 
     get 
     { 
      return this.classField; 
     } 
     set 
     { 
      this.classField = value; 
     } 
    } 

    /// <remarks/> 
    public OntologyClassAssertionNamedIndividual NamedIndividual 
    { 
     get 
     { 
      return this.namedIndividualField; 
     } 
     set 
     { 
      this.namedIndividualField = value; 
     } 
    } 
} 

/// <remarks/> 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] 
public partial class OntologyClassAssertionClass 
{ 

    private string iRIField; 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string IRI 
    { 
     get 
     { 
      return this.iRIField; 
     } 
     set 
     { 
      this.iRIField = value; 
     } 
    } 
} 

/// <remarks/> 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] 
public partial class OntologyClassAssertionNamedIndividual 
{ 

    private string iRIField; 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string IRI 
    { 
     get 
     { 
      return this.iRIField; 
     } 
     set 
     { 
      this.iRIField = value; 
     } 
    } 
} 

/// <remarks/> 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] 
public partial class OntologyDataPropertyAssertion 
{ 

    private OntologyDataPropertyAssertionDataProperty dataPropertyField; 

    private OntologyDataPropertyAssertionNamedIndividual namedIndividualField; 

    private OntologyDataPropertyAssertionLiteral literalField; 

    /// <remarks/> 
    public OntologyDataPropertyAssertionDataProperty DataProperty 
    { 
     get 
     { 
      return this.dataPropertyField; 
     } 
     set 
     { 
      this.dataPropertyField = value; 
     } 
    } 

    /// <remarks/> 
    public OntologyDataPropertyAssertionNamedIndividual NamedIndividual 
    { 
     get 
     { 
      return this.namedIndividualField; 
     } 
     set 
     { 
      this.namedIndividualField = value; 
     } 
    } 

    /// <remarks/> 
    public OntologyDataPropertyAssertionLiteral Literal 
    { 
     get 
     { 
      return this.literalField; 
     } 
     set 
     { 
      this.literalField = value; 
     } 
    } 
} 

/// <remarks/> 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] 
public partial class OntologyDataPropertyAssertionDataProperty 
{ 

    private string iRIField; 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string IRI 
    { 
     get 
     { 
      return this.iRIField; 
     } 
     set 
     { 
      this.iRIField = value; 
     } 
    } 
} 

/// <remarks/> 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] 
public partial class OntologyDataPropertyAssertionNamedIndividual 
{ 

    private string iRIField; 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string IRI 
    { 
     get 
     { 
      return this.iRIField; 
     } 
     set 
     { 
      this.iRIField = value; 
     } 
    } 
} 

/// <remarks/> 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] 
public partial class OntologyDataPropertyAssertionLiteral 
{ 

    private string datatypeIRIField; 

    private string valueField; 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string datatypeIRI 
    { 
     get 
     { 
      return this.datatypeIRIField; 
     } 
     set 
     { 
      this.datatypeIRIField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlTextAttribute()] 
    public string Value 
    { 
     get 
     { 
      return this.valueField; 
     } 
     set 
     { 
      this.valueField = value; 
     } 
    } 
} 
そして、このStudentクラス:

public class Student 
{ 
    public Student() 
    { 
    } 

    public string IRI { get; set; } 
    public OntologyDataPropertyAssertionNamedIndividual Name { get; set; } 
    public string TypeIRI { get; set; } 
    public string Value { get; set; } 
} 

そして、あなた件のデータをCにしている場合(あなた件のデータを取得するために、このLINQの例を使用します。 \ temp \ test.xml):

XmlSerializer serializer = new XmlSerializer(typeof(Ontology)); 

StreamReader reader = new StreamReader(@"c:\temp\test.xml"); 
Ontology ontology = (Ontology)serializer.Deserialize(reader); 

var students = from student in ontology.DataPropertyAssertion 
       select new Student() 
       { 
        Name = student.NamedIndividual, 
        TypeIRI = student.Literal.datatypeIRI, 
        Value = student.Literal.Value, 
        IRI = student.DataProperty.IRI, 
       }; 

foreach (var item in students) 
{ 
    //Use here your list of students 
} 

reader.Close(); 

reader.Dispose(); 
関連する問題