title: Is this an appropriate way to save a config file sorted by the 'key' attribute?
body: <p>I have an XML configuration file to which I will occasionally add new elements and, for ease of reading outside of the application, I want to keep it sorted alphabetically – by the "key" attribute of the "add" elements. </p>
  
  <p>After a lot of trial and error – using code I've gleaned from here, there and everywhere – I've come up with the code below. It appears to work okay, but I'm only just starting to learn Linq to XML, so I'd like to know whether this is an appropriate method or if there is a better/more efficient method I've missed. It just feels like I might be taking an unnecessary step in recreating the entire file element.</p>
  
  <p>Any guidance, thoughts or insights would be greatly appreciated.</p>
  
  <pre><code>    Dim newEx = cfgEx.&lt;appSettings&gt;(0)
      newEx.Add(&lt;add key=&lt;%= sNewKey %&gt; value=&lt;%= sValue %&gt;/&gt;)
      Dim sortEx = &lt;configuration&gt;
                       &lt;appSettings&gt;
                           &lt;%= From c In cfgEx.&lt;appSettings&gt;.&lt;add&gt; Order By c.@key Select c %&gt;
                       &lt;/appSettings&gt;
                   &lt;/configuration&gt;
      cfgEx.ReplaceAll(sortEx.Nodes)
      cfgEx.Save(sXmlConfigFile)
  </code></pre>
  
  <hr>
  
  <p><strong>EDIT</strong></p>
  
  <p>Okay, so I think I've worked out a better way to do this – without explicitly recreating all the parent tags in the<code>sortEx</code>variable:</p>
  
  <pre><code>    Dim newEx = cfgEx.&lt;appSettings&gt;(0)
      newEx.Add(&lt;add key=&lt;%= sNewKey %&gt; value=&lt;%= sValue %&gt;/&gt;)
      Dim sortEx = From c In newEx.&lt;add&gt; Order By c.@key Select c
      newEx.ReplaceAll(sortEx)
      cfgEx.Save(sXmlConfigFile)
  </code></pre>
  
  <p>So I guess the only question now is, is there anything at all problematic with <em>this</em> method?</p>