I have a XDocument where I\'d like to sort all of the elements alphabetically. Here\'s a simplified version of the structure:
I think these extension method work best.
public static class XmlLinq
{
public static void Sort(this XElement source, bool sortAttributes = true)
{
if (source == null)
throw new ArgumentNullException("source");
if (sortAttributes)
source.SortAttributes();
List sortedChildren = source.Elements().OrderBy(e => e.Name.ToString()).ToList();
source.RemoveNodes();
sortedChildren.ForEach(c => source.Add(c));
sortedChildren.ForEach(c => c.Sort());
}
public static void SortAttributes(this XElement source)
{
if (source == null)
throw new ArgumentNullException("source");
List sortedAttributes = source.Attributes().OrderBy(a => a.ToString()).ToList();
sortedAttributes.ForEach(a => a.Remove());
sortedAttributes.ForEach(a => source.Add(a));
}
}