解析OWL文件并将其保存到列表
问题描述:
我的存储对象从我的本体保存到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("")
}
}
}
在一个sentention:我怎样才能从“文字”标记中获取这些值?
答
您应该使用序列化&反序列化....
声明这个类(如果你要重建这个班,由例如,如果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();
此代码不会编译。没有空的构造函数为学生类,你不能分配一个LINQ查询声明为列表变量 –
你有看看https://github.com/bpellens/owldotnetapi –
@Sir Rufo 是的,我做了,但我需要做我自己的解析器,它是大学的小型项目。好吧,现在,当我有空的构造函数,并且我不会将查询分配给声明为List的变量(只是var studenci),那么结果是什么? – Kliwer