How to sort XML in LINQ C# by an attribute value? Also MVC

EDIT: Okay, I think you just want:

var sites = from s in xDoc.Element("sites").Elements("site")
            orderby (string) s.Attribute("name")
            select s;

Which could also be written as:

var sites = xDoc.Element("sites")
                .Elements("site")
                .OrderBy(s => (string) s.Attribute("name"));

Leave a Comment