用的XElement空值处理 [英] Dealing with XElement null value

查看:104
本文介绍了用的XElement空值处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我在查询XML。一个节点是丢失。所以,当我打电话XElement.Value我得到一个空的异常。

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 extension method, but I am wondering is there something build into the language for this?

推荐答案

使用空COALESCE运算符 ??

use the null coalesce operator ??

给定一个的XElement ELEM:

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

这篇关于用的XElement空值处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆