2017-06-17 62 views
0

我的存儲對象從我的本體保存到List中有問題。我在easy 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

在一個sentention:我怎樣才能從「文字」標記中獲取這些值?

+0

此代碼不會編譯。沒有空的構造函數爲學生類,你不能分配一個LINQ查詢聲明爲列表變量 –

+0

你有看看https://github.com/bpellens/owldotnetapi –

+0

@Sir Rufo 是的,我做了,但我需要做我自己的解析器,它是大學的小型項目。好吧,現在,當我有空的構造函數,並且我不會將查詢分配給聲明爲List的變量(只是var studenci),那麼結果是什麼? – Kliwer

回答

0

您應該使用序列化&反序列化....

聲明這個類(如果你要重建這個班,由例如,如果XML結構的變化,看here的@達米安 - Drygiel答案):

/// <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; 
     } 
    } 
} 

而這個學生類:

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; } 
} 

,並使用此LINQ例如爲讓您的DATAS(如果你的數據都是到C: \ 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(); 
相關問題