I have an XML file like this:

```
 <file name="picture1.jpg"/>
 <file name="file1.txt"/>
 <folder name="subfolder">
      <file name="picture2.jpg"/>
      <file name="file2.txt"/>
      <folder name="anotherfolder">
           <file name="file3.txt"/>
      </folder>
 </folder>
 <folder name="anotherfolder">
 </folder>

```

It needs to be sorted like this:

```
<folder name="anotherfolder">
 </folder>
 <file name="file1.txt"/>
 <file name="picture1.jpg"/>
 <folder name="subfolder">
      <folder name="anotherfolder">
           <file name="file3.txt"/>
      </folder>
      <file name="file2.txt"/>
      <file name="picture2.jpg"/>
 </folder>

```

The code I have to sort is:

```
public static XDocument Sort(XDocument file)
    {
        return new XDocument(Sort(file.Root));
    }
    private static XElement Sort(XElement element)
    {
        XElement newElement = new XElement(element.Name,
            from child in element.Elements()
            orderby child.Name.ToString()
            select Sort(child));
        if (element.HasAttributes)
        {
            foreach (XAttribute attrib in element.Attributes())
            {
                newElement.SetAttributeValue(attrib.Name, attrib.Value);
            }
        }
        return newElement;
    }

```

It does the job...but only partly. It sorts FILE based on name attribute separately from FOLDER. I would like them to be considered together for the sorted xml. What should be done?