Dealing with XElement null value

大城市里の小女人 提交于 2019-12-12 18:30:52

问题


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?


回答1:


use the null coalesce operator ??

Given an XElement elem:

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


来源:https://stackoverflow.com/questions/2057281/dealing-with-xelement-null-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!