Notice in this code I am trying to check for the existence of the rdfs:range element before trying to select it. I do this to avoid a possible null reference exception at runtime.
private readonly XNamespace rdf = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
private readonly XNamespace rdfs = "http://www.w3.org/2000/01/rdf-schema#";
private readonly XElement ontology;
public List<MetaProperty> MetaProperties
{
get
{
return (from p in ontology.Elements(rdf + "Property")
select new MetaProperty
{
About = p.Attribute(rdf + "about").Value,
Name = p.Element(rdfs + "label").Value,
Comment = p.Element(rdfs + "comment").Value,
RangeUri = p.Elements(rdfs + "range").Count() == 1
? p.Element(rdfs + "range").Attribute(rdf + "resource").Value
: null
}).ToList();
}
}
This is kinda bugging me, what I really want to do is something like this:
RangeUri = p.HasElements(rdfs + "range")
? p.Element(rdfs + "range").Attribute(rdf + "resource").Value
: null
However there is no
p.HasElement(string elementName)
method available.
I guess I could create a method extension to do this, but am wondering if there is something already built in or if there are other ways to do this?