XElement 上的 Null 检查
我当前的项目(C# 3.5)有很多这样的代码(elem 是 XElement 的实例):
textbox1.Text = elem.Element("TagName") == null ? "" : elem.Element("TagName").Value;
有没有办法在不重复调用 elem.Element() 且不使用扩展方法的情况下编写相同的内容? 也许使用 lambda 表达式? (但我不知道怎么做。)
My current project (C# 3.5) has a lot of code like this (elem is an instance of XElement):
textbox1.Text = elem.Element("TagName") == null ? "" : elem.Element("TagName").Value;
Is there any way to write the same thing without repeating a call elem.Element() and without use of extension methods?
Maybe using lambdas? (But I cannot figure out how.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
XElement 具有到 String(以及一堆其他类型)的显式转换,实际上会调用 .价值。
换句话说,你可以这样写:
我认为如果实际元素也为 null
-编辑-
验证,这将返回 null,
这是一个示例:
此后
n
将为 null。XElement has a explicit conversion to String (and a bunch of other types) that will actually call .Value.
In otherwords you can write this:
i think this will return null if the actual element is null as well
-edit-
verified,
here is an example:
n
will be null after this.是的。你可以这样写:
这是空合并运算符。
这意味着如果左侧不为空,则使用左侧。如果为空,则使用右侧。
Yes. you can write it like this:
This is the null coalescing operator.
It means that if the left hand side is not null, then use the left hand side. If it is null, then use the right hand side.
CodeProject 上有一篇很棒的文章介绍了此类操作:
http://www.codeproject.com/KB/cs/maybemonads.aspx
CodeProject 上提供了其他示例。
There is a great article on the CodeProject for such actions:
http://www.codeproject.com/KB/cs/maybemonads.aspx
Other examples are available on the CodeProject.
疯狂的
??
方法:不过,我更喜欢扩展方法。
A crazy
??
approach:Would have preferred an extension method though.
与此类似的情况与 Xml 布尔检查有关,这是我使用空合并运算符的解决方案:
Convert.ToBoolean(this.xDocument.Root.XPathSelectElement(@"/settings/checkbox[@name = 'MessageArrivedTimeCheckBox']").Value ?? "false");
原因是 xml 默认情况下被理解为字符串,存在类型解析问题,可以通过 xml 本身内的序列化参数来解决,但我怀疑这是否可能,因为它直到运行时才理解它。也许可以通过类型期望对元素本身进行声明或赋值?
Have a similar situation to this which has to do with Xml boolean checks and this was my resolution that used the null coalescing operator:
Convert.ToBoolean(this.xDocument.Root.XPathSelectElement(@"/settings/checkbox[@name = 'MessageArrivedTimeCheckBox']").Value ?? "false");
The reason being that the xml is understood by default as a string with there being type resolution issues that may be resolvable through serialization parameters within the xml itself, but I doubt even that would be possible because it doesn't understand it until runtime. Maybe there's a declaration or assignment that can be done with the element itself with type expectation?
这不适用于原始问题,但由于该问题似乎仍在积极查看,因此这是 C# 6.0+ 项目的更新:
自 C# 6.0 起,使用 null 条件运算符 (
?.
) 和空合并运算符 (??
) 你可以这样做:Cf. 空条件运算符文档 和 空合并运算符文档。
This doesn't apply to the original question, but since it seems this question is still actively viewed, this is an update for C# 6.0+ projects:
Since C# 6.0, using the null-conditional operator (
?.
) and null-coalescing operator (??
) you can do this:Cf. Null-Conditional Operator Documentation and Null-Coalescing Operator Documentation.