title: Checking for null in dynamic elements
body: <p>I am loading a string into an xml element and have instituted what I thought what adequate error checking, however if a particular xml node isnt present it still throws an error </p>
  
  <pre><code>StackTrace "at System.Linq.Enumerable.First[TSource](IEnumerable`1 source)\r\n 
  at MvcApplication1.Controllers.MyController.MySearch(SearchModel s)
  in c:\\...Controller.cs:line 2900" string e {"Sequence contains no elements"}   
  System.Exception {System.InvalidOperationException}
  </code></pre>
  
  <p>If this isnt the correct way to check for the existence of a node, what is? Please advise and thanx in advance.</p>
  
  <pre><code>      XmlDocument doc = new XmlDocument();
              doc.LoadXml(mytextstring);
              XDocument docx = DocumentToXDocumentReader(doc);
      ...
              try
              {
                  foreach (XElement xe in docx.Descendants("RootElement"))
                  {
  
                      MySearchModel jbcs = new MySearchModel();
  
                      jbcs.ID = xe.Attribute("SID").Value;                        
                      ...
                      jbcs.FirstName = (xe.Element("PersonalData").Element("Name").Element("First") == null) ? "" : 
                      xe.Element("PersonalData").Element("Name").Element("First").Value.Trim();
  
                      jbcs.LastName = (xe.Element("PersonalData").Element("Name").Element("Last") == null) ? "" :
                      xe.Element("PersonalData").Element("Name").Element("Last").Value.Trim();  
  
                      jbcs.HomeLocation = (xe.Element("PersonalData").Element("Address").Element("Location") == null) ? "" :
                      xe.Element("PersonalData").Element("Address").Element("Location").Value.Trim(); 
          ...
         }
      }
  </code></pre>
  
  <p>This doesnt work either:</p>
  
  <pre><code>jbcs.FirstName = (xe.Element("PersonalData").Element("Name").Element("First").Equals(null)) ? ...
  </code></pre>