【发布时间】:2014-08-13 17:20:36
【问题描述】:
我想使用 XDocument 访问 XML 文档中的一些深层结构。
为了避免 NULL 异常,所以我有很多这样的 IF 检查
if(doc.Root.Element("E1") != null)
{
if(doc.Root.Element("E1").Element("E2") != null)
{
if(doc.Root.Element("E1")
.Element("E2")
.SingleOrDefault(e => e.Attribute("id") != null &&
e.Attribute("id").Equals("ABC")) != null)
{
var n = doc.Root
.Element("E1")
.Element("E2")
.SingleOrDefault(e => e.Attribute("id") != null &&
e.Attribute("id").Equals("ABC"))
.Attribute("name").Value;
}
}
}
实际结构要深得多。我能以某种方式消除这些 NULL 检查吗?
【问题讨论】:
标签: c# xml linq if-statement null