title: How to check an element exist in XML
body: <pre><code>&lt;library&gt;
      &lt;book&gt;
        &lt;id&gt;1&lt;/id&gt;
        &lt;name&gt;abc&lt;/name&gt;
           &lt;read&gt;
            &lt;data&gt;yes&lt;/data&gt;
            &lt;num&gt;20&lt;/num&gt;
          &lt;/read&gt;
      &lt;/book&gt;
      &lt;book&gt;
        &lt;id&gt;20&lt;/id&gt;
        &lt;name&gt;xyz&lt;/name&gt;
           &lt;read&gt;
            &lt;data&gt;yes&lt;/data&gt;
          &lt;/read&gt;
      &lt;/book&gt;
      &lt;book&gt;
        &lt;id&gt;30&lt;/id&gt;
        &lt;name&gt;ddd&lt;/name&gt;
      &lt;/book&gt;
  &lt;/library&gt;
  </code></pre>
  
  <p>From this I am reading the <code>&lt;book&gt;</code> node with element <code>&lt;id&gt;</code> value = 20 using below code</p>
  
  <pre><code>XElement root = XElement.Load("e_test.xml")
  XElement book = root.Elements("book")
                      .Where(x =&gt; (int) x.Element("id") == 20)
                      .SingleOrDefault();
  if (book == null)
  {
      // No book with that ID
  }
  
  
  
  if(book.Element("read").Element("num") != null) //check the node exist
  {
      int num = (int) book.Element("read").Element("num");
  }
  </code></pre>
  
  <p>Here the if condition is not working correctly. It is passing the condition and get inside and giving null exception. Is this the right way to check the same?</p>
  
  <p>I am using .NET FRAMEWORK 4.0</p>