public static void Main()
var root = XElement.Parse(xml);
var newRoot = CopyAndSort(root);
Console.WriteLine(newRoot);
private static XElement CopyAndSort(XElement e)
var elements = e.Elements()
.OrderBy(x => (string)x.Attribute("aa"))
return new XElement(e.Name, e.Attributes(), elements);