How to sort XML elements in parent element?

I need to change the order of elements in one parent element. So it is easy to get sorted elements.

myXML.Elements("Issue").OrderBy(x => int.Parse(x.Attribute("id").Value));

But I need to return this sorted collection back in myXML XElement without affecting it's structure.

How to do this?

Jon Skeet
people
quotationmark

It sounds like you just want:

// No need to specify "Issue" if that's all that's in myXML. Prefer explicit
// attribute conversion over "manual" parsing
var orderedIssues = myXML.Elements().OrderBy(x => (int) x.Attribute("id"));
myXML.ReplaceNodes(orderedIssues);

people

See more on this question at Stackoverflow