开发者

Dealing with XElement null value

开发者 https://www.devze.com 2022-12-16 10:43 出处:网络
I have an xml that I am querying.One of the nodes is missing.So when I call XElement.Value I get a null exception.

I have an xml that I am querying. One of the nodes is missing. So when I call XElement.Value I get a null exception.

What is the best way to guard against this? I know I can write an exten开发者_高级运维sion method, but I am wondering is there something build into the language for this?


use the null coalesce operator ??

Given an XElement elem:

var value = (string)elem ?? "empty";
0

精彩评论

暂无评论...
验证码 换一张
取 消