splash27
splash27

Reputation: 2107

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?

Upvotes: 0

Views: 132

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500675

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);

Upvotes: 2

Related Questions