title: XmlDocument sort nodes by the inner text value of a child
body: <p>i want to sort the nodes called ImageInfo by the number in the pos node because i have buttons that change the position up or down and i need to sort the ImageInfo node in the correct order when the pos has changed.</p>
  
  <p>i apologise ahead for not having any c# code but i assure you that i have tried so many different things and im in need of help.</p>
  
  <p>here is my xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;MplAndSiImages xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
    &lt;MplImages&gt;
      &lt;ImageInfo&gt;
        &lt;pos&gt;1&lt;/pos&gt;
        &lt;Name&gt;1.png&lt;/Name&gt;
        &lt;ParentObjectId&gt;b66a23a8-6268-e611-80e2-c4346bad02e8&lt;/ParentObjectId&gt;
        &lt;Url&gt;http://localhost:8080/b66a23a8-6268-e611-80e2-c4346bad02e8/1.png&lt;/Url&gt;
      &lt;/ImageInfo&gt;
      &lt;ImageInfo&gt;
        &lt;pos&gt;2&lt;/pos&gt;
        &lt;Name&gt;2.png&lt;/Name&gt;
        &lt;ParentObjectId&gt;b66a23a8-6268-e611-80e2-c4346bad02e8&lt;/ParentObjectId&gt;
        &lt;Url&gt;http://localhost:8080/b66a23a8-6268-e611-80e2-c4346bad02e8/2.png&lt;/Url&gt;
      &lt;/ImageInfo&gt;
      &lt;ImageInfo&gt;
        &lt;pos&gt;3&lt;/pos&gt;
        &lt;Name&gt;3.png&lt;/Name&gt;
        &lt;ParentObjectId&gt;b66a23a8-6268-e611-80e2-c4346bad02e8&lt;/ParentObjectId&gt;
        &lt;Url&gt;http://localhost:8080/b66a23a8-6268-e611-80e2-c4346bad02e8/3.png&lt;/Url&gt;
      &lt;/ImageInfo&gt;
    &lt;/MplImages&gt;
    &lt;SiImages /&gt;
  &lt;/MplAndSiImages&gt;
  </code></pre>
  
  <p>here is my c# code:</p>
  
  <p>it is called on the click of an action link button and i need it to change the poition to 1 less to move it up in the list and i have the number change but the xml need s to be sorted so it has the ImageInfo nodes in the correct order.</p>
  
  <pre><code>public ActionResult MoveUp(string name, string id)
  {
  
      var pathConfig = WebConfigurationManager.AppSettings["ProductImageFolderPath"];
      var url = pathConfig + id + "\\" + "ModelConfig.xml";
  
      XmlDocument doc = new XmlDocument();
  
      doc.Load(url);
      XmlNode root = doc.DocumentElement;
      XmlNode upNode = root.SelectSingleNode("/MplAndSiImages/MplImages/ImageInfo[Name/text() = '" + name + "']/pos");
  
      string upNodeValue = upNode.InnerText;
      int upNodeInt = Int32.Parse(upNodeValue);
      upNodeInt = upNodeInt - 1;
      var upNodeString = upNodeInt.ToString();
      upNode.InnerText = upNodeString;
  
      XmlNode downNode = root.SelectSingleNode("/MplAndSiImages/MplImages/ImageInfo/pos[text() = '" + upNodeString + "']");
  
      string downNodeValue = downNode.InnerText;
      int downNodeInt = Int32.Parse(downNodeValue);
      downNodeInt = downNodeInt + 1;
      var downNodeString = downNodeInt.ToString();
      downNode.InnerText = downNodeString;
  
      Func&lt;string, int&gt; ParseIntOrDefault = (string input) =&gt;
      {
          int output;
          int.TryParse(input, out output);
          return output;
      };
  
      var result = doc.SelectNodes("MplAndSiImages/MplImages/*")
          .Cast&lt;XmlNode&gt;()
          .OrderBy(element =&gt; element.SelectSingleNode("pos").InnerText)
          .ToList();
  
  
      doc.Save(url);
      return RedirectToAction("UploadAnImage", new { id = id });
  }
  </code></pre>
  
  <p>I have seen this and tried it but is there any way of doing this with xmldocument:</p>
  
  <pre><code>XElement root = XElement.Load(xmlfile);
  var orderedtabs = root.Elements("Tab")
                        .OrderBy(xtab =&gt; (int)xtab.Element("Order"))
                        .ToArray();
  root.RemoveAll();
  foreach(XElement tab in orderedtabs)
      root.Add(tab);
  root.Save(xmlfile);
  </code></pre>
  
  <p>I am ordering the images to display on a web page.
  and when the move up button is pressed the image will be moved up in the list and swap places with the image above it.</p>