title: Set element attribute value in XML with Linq
body: <p>I have the following XML:</p>
  
  <pre><code>&lt;appsettings&gt;
  
      &lt;add key="MDXQueryDirectory" value="" /&gt;
  
      &lt;add key="URLPrefix" value="/Manager" /&gt;
  
  &lt;/appsettings&gt;
  </code></pre>
  
  <p>and code:</p>
  
  <pre><code> XElement doc = XElement.Load(@"config_path");
  
  
   var element = doc.Elements("add")
              .SingleOrDefault(x =&gt; x.Attribute("key").Value == "MDXQueryDirectory");
  
  
   element.SetAttributeValue("value", txtMDXQueryDirectory.Text);
  </code></pre>
  
  <p>I want to assign the txtMDXQueryDirectory textbox data to the MDXQueryDirectory appSsetting. First I use <code>Single</code> method but it doesn't work and I researched on google I found this <a href="http://msmvps.com/blogs/kevinmcneish/archive/2009/12/17/fixing-linq-error-sequence-contains-no-elements.aspx" rel="nofollow">page</a> so bugs solve related that article. But know <code>SingleOrDefault</code> method doesn't work. It throws <code>NullPointerException (Object reference not set to an instance an object)</code></p>
  
  <p>How can I solve problem or maybe you suggest different idiom to set value data in xml?</p>