I want to access some deep structures in an XML document using XDocument.
To avoid NULL exceptions, so I have many IF checks like those
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;
}
}
}
The actual structure is way deeper. Can I eliminate these NULL checks somehow?