如何使用XElement检查元素是否存在?

时间:2022-11-17 20:24:54

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.

请注意,在此代码中,我尝试在尝试选择它之前检查rdfs:range元素是否存在。我这样做是为了避免在运行时出现可能的空引用异常。

    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:

这有点烦我,我真正想做的是这样的:

p.HasElements(rdfs + "range") ?
    p.Element(rdfs + "range").Attribute(rdf + "resource").Value :
    null

However there is no HasElement(string elementName) method available.

但是,没有可用的HasElement(string elementName)方法。

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?

我想我可以创建一个方法扩展来执行此操作,但我想知道是否已经内置了某些内容或是否有其他方法可以执行此操作?

2 个解决方案

#1


1  

Same basic thing, but neater

同样基本的东西,但更整洁

return (from p in ontology.Elements(rdf + "Property") 
let xRange = p.Element(rdfs + "range") 
select new MetaProperty 
{ 
    About = p.Attribute(rdf + "about").Value, 
    Name = p.Element(rdfs + "label").Value, 
    Comment = p.Element(rdfs + "comment").Value, 
    RangeUri = xRange == null ? null : xRange.Attribute(rdf + "resource").Value 
}).ToList(); 

#2


7  

You can use:

您可以使用:

p.Elements(rdfs + "range").SingleOrDefault()

which will return null if there are no elements. It will throw an exception if there's more than one matching element - FirstOrDefault() will avoid this if it's not the desired behaviour.

如果没有元素,它将返回null。如果存在多个匹配元素,它将抛出异常 - 如果不是所需的行为,FirstOrDefault()将避免这种情况。

EDIT: As per my comment, and taking advantage of the conversion from XAttribute to string also handling nulls:

编辑:根据我的评论,并利用从XAttribute转换为字符串也处理空值:

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 = (string) p.Elements(rdf + "range")
                                            .Attributes(rdf + "resource")
                                            .FirstOrDefault()
                    }).ToList(); 

If you have the same thing in many places, you could write an extension method to encapsulate that very easily:

如果你在很多地方都有相同的东西,你可以编写一个扩展方法来非常容易地封装它:

public static XAttribute FindAttribute(this XElement element,
    XName subElement, XName attribute)
{
    return element.Elements(subElement).Attributes(attribute).FirstOrDefault();
}

So the RangeUri bit would be:

那么RangeUri位将是:

RangeUri = (string) p.FindAttribute(rdf + "range", rdf + "resource")

#1


1  

Same basic thing, but neater

同样基本的东西,但更整洁

return (from p in ontology.Elements(rdf + "Property") 
let xRange = p.Element(rdfs + "range") 
select new MetaProperty 
{ 
    About = p.Attribute(rdf + "about").Value, 
    Name = p.Element(rdfs + "label").Value, 
    Comment = p.Element(rdfs + "comment").Value, 
    RangeUri = xRange == null ? null : xRange.Attribute(rdf + "resource").Value 
}).ToList(); 

#2


7  

You can use:

您可以使用:

p.Elements(rdfs + "range").SingleOrDefault()

which will return null if there are no elements. It will throw an exception if there's more than one matching element - FirstOrDefault() will avoid this if it's not the desired behaviour.

如果没有元素,它将返回null。如果存在多个匹配元素,它将抛出异常 - 如果不是所需的行为,FirstOrDefault()将避免这种情况。

EDIT: As per my comment, and taking advantage of the conversion from XAttribute to string also handling nulls:

编辑:根据我的评论,并利用从XAttribute转换为字符串也处理空值:

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 = (string) p.Elements(rdf + "range")
                                            .Attributes(rdf + "resource")
                                            .FirstOrDefault()
                    }).ToList(); 

If you have the same thing in many places, you could write an extension method to encapsulate that very easily:

如果你在很多地方都有相同的东西,你可以编写一个扩展方法来非常容易地封装它:

public static XAttribute FindAttribute(this XElement element,
    XName subElement, XName attribute)
{
    return element.Elements(subElement).Attributes(attribute).FirstOrDefault();
}

So the RangeUri bit would be:

那么RangeUri位将是:

RangeUri = (string) p.FindAttribute(rdf + "range", rdf + "resource")