title: XElement with LINQ Select and optional elements
body: <p>I am trying to display some date from some XML I get from an external service. I am using XElement and I try to use LINQ select to get my data.</p>
  
  <pre><code>var xElem = XElement.Load(HttpUtility.UrlPathEncode(url));
  var books = (from pubs in xElem.Elements("result")
               select new
               {
                   Id = (string)pubs.Element("data").Element("id"),
                   Title = (string)pubs.Element("data").Element("title"),
                   Year = (string)pubs.Element("data").Element("year"),
                   Resources = (string)pubs.Element("data")
                                           .Element("resource")
                                           .Element("url")
                                           .ElementValueNull(),
                   Authors= pubs.Element("data").Elements("person")
               }).ToList();
  foreach (var book in books)
  {
      // Put the string together with string builder....
      foreach (var person in book.Authors)
      {
          //Get the authors
      }
  }
  </code></pre>
  
  <p>And of course I have made the class for ElementValueNull.</p>
  
  <pre><code>//This method is to handle if element is missing
  public static string ElementValueNull(this XElement element)
  {
      if (element != null)
          return element.Value;
  
      return "";
  }
  
  //This method is to handle if attribute is missing
  public static string AttributeValueNull(this XElement element, string attributeName)
  {
      if (element == null)
          return "";
      else
      {
          XAttribute attr = element.Attribute(attributeName);
          return attr == null ? "" : attr.Value;
      }
  }
  </code></pre>
  
  <p>The problem is that the resource tag with it's elements are not always present. And if it isn't there it will skip the whole record. Is there any easy way of making it so that it will just make the Resources have the empty string returned from my class but still add the record still using a LINQ select?</p>
  
  <p>EDIT with XML example:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;tester xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://tester.no/xmlSchema/xsd/tester.xsd"&gt;
    &lt;generert&gt;2014-12-01&lt;/generert&gt;
    &lt;result&gt;
      &lt;data&gt;
        &lt;id&gt;297474&lt;/id&gt;
        &lt;person&gt;
          &lt;id&gt;11690&lt;/id&gt;
          &lt;surname&gt;Medel-Svensson&lt;/surname&gt;
          &lt;firstname&gt;Ronnie&lt;/firstname&gt;
        &lt;/person&gt;
        &lt;title&gt;Title 1&lt;/title&gt;
        &lt;year&gt;2009&lt;/year&gt;
      &lt;/data&gt;
    &lt;/result&gt;
    &lt;result&gt;
      &lt;data&gt;
        &lt;id&gt;807059&lt;/id&gt;
        &lt;person&gt;
          &lt;id&gt;11690&lt;/id&gt;
          &lt;surname&gt;Bronskimlet&lt;/surname&gt;
          &lt;firstname&gt;Hallstein&lt;/firstname&gt;
        &lt;/person&gt;
        &lt;person&gt;
          &lt;id&gt;328009&lt;/id&gt;
          &lt;surname&gt;Kroksleiven&lt;/surname&gt;
          &lt;firstname&gt;Jostein&lt;/firstname&gt;
        &lt;/person&gt;
        &lt;person&gt;
          &lt;id&gt;328010&lt;/id&gt;
          &lt;surname&gt;Gassolini&lt;/surname&gt;
          &lt;firstname&gt;Ruffino&lt;/firstname&gt;
        &lt;/person&gt;
        &lt;person&gt;
          &lt;id&gt;327990&lt;/id&gt;
          &lt;surname&gt;von Schnellfahrer&lt;/surname&gt;
          &lt;firstname&gt;Heinrich&lt;/firstname&gt;
        &lt;/person&gt;
        &lt;title&gt;Title 2&lt;/title&gt;
        &lt;year&gt;2010&lt;/year&gt;
        &lt;resource&gt;
          &lt;type&gt;
            &lt;code&gt;TEXT&lt;/code&gt;
          &lt;/type&gt;
          &lt;url&gt;http://www.example.com/&lt;/url&gt;
        &lt;/resource&gt;
      &lt;/data&gt;
    &lt;/result&gt;
    &lt;result&gt;
      &lt;data&gt;
        &lt;id&gt;1164653&lt;/id&gt;
        &lt;person&gt;
          &lt;id&gt;11690&lt;/id&gt;
          &lt;surname&gt;Bergsprekken&lt;/surname&gt;
          &lt;firstname&gt;Mysil&lt;/firstname&gt;
        &lt;/person&gt;
        &lt;title&gt;Title 3&lt;/title&gt;
        &lt;year&gt;2014&lt;/year&gt;
        &lt;resource&gt;
          &lt;type&gt;
            &lt;code&gt;FULLTEKST&lt;/code&gt;
          &lt;/type&gt;
          &lt;url&gt;http://www.example.com/&lt;/url&gt;
        &lt;/resource&gt;
      &lt;/data&gt;
    &lt;/result&gt;
  &lt;/tester&gt;
  </code></pre>