[英]Check if a specific Attributes exist in an Element
i need to check if a specific Attributes ConnectionInd
exists in the Element of FlightSegment
. 我需要检查FlightSegment
元素中FlightSegment
存在特定的属性ConnectionInd
。 If it exist, i will store the value, if it doesnt, i will move on and read the next attributes. 如果存在,则将存储该值,如果不存在,则将继续并读取下一个属性。
The following is if it exist: 如果存在,则为以下内容:
<FlightSegment ArrivalDateTime="06-16T06:10" ConnectionInd="O" DepartureDateTime="2013-06-16T00:15" SmokingAllowed="false" eTicket="true">
<Destination ... />
</FlightSegment>
And this is if it doesn't exist: 这是不存在的情况:
<FlightSegment ArrivalDateTime="03-27T17:35" DepartureDateTime="2013-03-27T13:30" SmokingAllowed="false" eTicket="true">
<Destination ... />
</FlightSegment>
i'm checking it with the following code but when there's no ConnectionInd
, it will throw an error saying Object reference not set to an instance of an object.
我正在使用以下代码进行检查,但是当没有ConnectionInd
,它将抛出错误,提示Object reference not set to an instance of an object.
if (FlightSegment.Item(f).Attributes["ConnectionInd"].Value != "" && FlightSegment.Item(f).Attributes["ConnectionInd"] != null)
{
string conInd = FlightSegment.Item(f).Attributes["ConnectionInd"].Value;
}
Vice versa: 反之亦然:
First check 第一次检查
FlightSegment.Item(f).Attributes["ConnectionInd"] != null
then 然后
FlightSegment.Item(f).Attributes["ConnectionInd"].Value != ""
Otherwise if it's indeed null, you will get NullReferenceException which you're actually getting. 否则,如果它确实为null,则将获得实际上得到的NullReferenceException。
More efficient: 更高效:
var att = FlightSegment.Item(f).Attributes["ConnectionInd"];
if (var != null)
{
string cind = att.Value;
}
应该有一个像hasAttribute之类的方法或类似的方法,而不是FlightSegment.Item(f).Attributes [“ ConnectionInd”]。Value,因为如果“ ConnectionInd”不存在,您将试图获取“ Value”空对象的值,这将导致异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.