title: Sort XMLDocument using linq
body: <p>I can't seem to figure out how to do this. There are several other examples on here, but nothing really matches what I want to do:</p>
  
  <p>Consider the following XMLDocument object:</p>
  
  <pre><code>&lt;Policy&gt;
      &lt;Covers&gt;
          &lt;MyCover1&gt;
              &lt;properties&gt;
                  &lt;sortOrder&gt;1&lt;/sortOrder&gt;
              &lt;/properties&gt;
          &lt;/MyCover1&gt;
  
          &lt;MyCover3&gt;
              &lt;properties&gt;
                  &lt;sortOrder&gt;3&lt;/sortOrder&gt;
              &lt;/properties&gt;
          &lt;/MyCover3&gt;
  
          &lt;MyCover2&gt;
              &lt;properties&gt;
                  &lt;sortOrder&gt;2&lt;/sortOrder&gt;
              &lt;/properties&gt;
          &lt;/MyCover2&gt;
      &lt;/Covers&gt;
  &lt;/Policy&gt;
  </code></pre>
  
  <p>How would I go about in sorting this document based on node "sortOrder" using linQ or another method?</p>
  
  <p>After the sort the outerxml should basically look like this:</p>
  
  <pre><code>&lt;Policy&gt;
      &lt;Covers&gt;
          &lt;MyCover1/&gt;
          &lt;MyCover2/&gt;
          &lt;MyCover3/&gt;
      &lt;/Covers&gt;
  &lt;/Policy&gt;
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I've made some progress, the data is now sorted, but how do I update the original unsorted XmlDocument? This is what I have so far:</p>
  
  <pre><code>private static void DoSort(XmlDocument policyDocument)
  {
      foreach(XmlNode coverGroup in policyDocument.SelectNodes("//CoverGroup"))
      {
          XDocument test = XDocument.Parse(coverGroup.OuterXml);
          var sorted = from xe in test.Element("CoverGroup").Elements()
               let so = xe.Element("properties").Element("displayOrder")
               let num = (int)so
               orderby num
               select xe;     
  
          var result = new XElement("CoverGroup", sorted);
      }
  }
  </code></pre>
  
  <p>I need to apply the changes back to "policyDocument". Note: A cover can have a CoverGroup of it's own, which can then again have Covers with CoverGroups of it's own. This goes down at least 4 levels: ie</p>
  
  <pre><code>&lt;Policy&gt;
  &lt;Covers&gt;
      &lt;MyCover1&gt;
          &lt;properties&gt;
              &lt;sortOrder&gt;&lt;/sortOrder&gt;
          &lt;/properties&gt;
          &lt;CoverGroup&gt;
              &lt;MyCover1Child&gt;
                  &lt;properties&gt;
                      &lt;sortOrder&gt;&lt;/sortOrder&gt;
                  &lt;/properties&gt;
              &lt;/MyCover1Child&gt;
          &lt;/CoverGroup&gt;
      &lt;/MyCover1&gt;
      ...
  &lt;/Covers&gt;
  </code></pre>
  
  <p></p>
  
  <p>The XPATH and foreach above cathers for the above structure, so I was thinking of maybe just replacing the coverGroup XmlNode in the first foreach with the new sorted list, but I dont know how to reconstruct a new XmlNode. IF we can figure this out, then we can simply do this:</p>
  
  <pre><code>policyDocument.ReplaceChild(coverGroup, mySortedXmlNode)
  </code></pre>