title: Why does this throw a NullReferenceException?
body: <pre><code>private void alterNodeValue(string xmlFile, string parent, string node, string newVal)
  {
      XDocument xml = XDocument.Load(this.dir + xmlFile);
  
      if (xml.Element(parent).Element(node).Value != null)
      {
          xml.Element(parent).Element(node).Value = newVal;
      }
      else
      {
          xml.Element(parent).Add(new XElement(node, newVal)); 
      }
  
      xml.Save(dir + xmlFile); 
  }  
  </code></pre>
  
  <p>Why does this throw</p>
  
  <blockquote>
    <p>System.NullReferenceException was unhandled by user code</p>
  </blockquote>
  
  <p>on this line </p>
  
  <pre><code>if (xml.Element(parent).Element(node).Value != null)
  </code></pre>
  
  <p>?</p>
  
  <p>I'm guessing it's because the XML node doesn't exist, but that's what the <code>!= null</code> is suppose to check for. How do I fix that?</p>
  
  <p>I've tried several things and they ALL throw the same exception at some point during the not null check. </p>
  
  <p>Thanks for any help. </p>