0

I have a problem with save object from my ontology into List. I did easy example on easy xml (not owl file), but now I have no idea how can I get these values for single student.

Ontology fragment:

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

I want take values form "Literal" marker and save them into list for student_3, 4, 5 etc as Student.

Student:

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

Yesterday I spent almost half a day on rehearsals, now I'm with something like this:

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 )

In one sentention: How can I get these values from "Literal" marker?

Kliwer
  • 13
  • 3
  • This code will never compile. No empty constructor for Student class and you cannot assign a LINQ query to a variable declared as a List – Sir Rufo Jun 17 '17 at 07:22
  • Did you have had a look at https://github.com/bpellens/owldotnetapi – Sir Rufo Jun 17 '17 at 07:30
  • @Sir Rufo Yes, I did, but I need to do my own parser, it is small project for university. Ok, now, when I have empty constructor and I won't assign query to variable declared as a List (just var studenci), what's then? – Kliwer Jun 17 '17 at 07:55
  • Well NamedIndividual is **not** an attribute of the DataPropertyAssertion elements. It is an Element with an IRI attribute and that value is never equal to "Student" – Sir Rufo Jun 17 '17 at 08:04

1 Answers1

0

You should use Serialization & Deserialization....

Déclare this classes (if you want rebuild this classes, by example if XML structure change, look here, answer of @Damian-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;
        }
    }
}

And this Student class :

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

And use this linq example for get your datas (if your datas are into 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();
Esperento57
  • 16,521
  • 3
  • 39
  • 45