How to Sort XDocument Content by Node Name

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi folks,
Please can you help with sorting elements in an xml file...Im new to LINQ to XML.
I have the following file:
<pre style="font-family:Consolas; background-color:white <span style="color:blue <<span style="color:#a31515 my_elem<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 Toms_elem<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 Kates_elem<span style="color:blue />
<span style="color:blue <<span style="color:#a31515 Als_elem<span style="color:blue />
<span style="color:blue <<span style="color:#a31515 your_elem<span style="color:blue />
<span style="color:blue <<span style="color:#a31515 another_elem<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 some_elem<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 num3<span style="color:blue >13<span style="color:blue </<span style="color:#a31515 num3<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 num1<span style="color:blue >14<span style="color:blue </<span style="color:#a31515 num1<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 num2<span style="color:blue >15<span style="color:blue </<span style="color:#a31515 num2<span style="color:blue >
<span style="color:blue </<span style="color:#a31515 some_elem<span style="color:blue >
<span style="color:blue </<span style="color:#a31515 another_elem<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 sams_elem<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 item_elem<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 elem6<span style="color:blue >1<span style="color:blue </<span style="color:#a31515 elem6<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 elem4<span style="color:blue >2<span style="color:blue </<span style="color:#a31515 elem4<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 elem2<span style="color:blue >3<span style="color:blue </<span style="color:#a31515 elem2<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 elem1<span style="color:blue >4<span style="color:blue </<span style="color:#a31515 elem1<span style="color:blue >
<span style="color:blue </<span style="color:#a31515 item_elem<span style="color:blue >
<span style="color:blue </<span style="color:#a31515 sams_elem<span style="color:blue >
<span style="color:blue </<span style="color:#a31515 Toms_elem<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 dudes_elem<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 Kates_elem<span style="color:blue />
<span style="color:blue <<span style="color:#a31515 Als_elem<span style="color:blue />
<span style="color:blue <<span style="color:#a31515 dummy_elem<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 c_elem<span style="color:blue >cde<span style="color:blue </<span style="color:#a31515 c_elem<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 a_elem<span style="color:blue >abc<span style="color:blue </<span style="color:#a31515 a_elem<span style="color:blue >
<span style="color:blue </<span style="color:#a31515 dummy_elem<span style="color:blue >
<span style="color:blue </<span style="color:#a31515 dudes_elem<span style="color:blue >
<span style="color:blue </<span style="color:#a31515 my_elem<span style="color:blue >[/code]
I need all elements sorted by name, as follows:
<pre style="font-family:Consolas; background-color:white <span style="color:blue <<span style="color:#a31515 my_elem<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 dudes_elem<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 Als_elem<span style="color:blue />
<span style="color:blue <<span style="color:#a31515 dummy_elem<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 a_elem<span style="color:blue >abc<span style="color:blue </<span style="color:#a31515 a_elem<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 c_elem<span style="color:blue >cde<span style="color:blue </<span style="color:#a31515 c_elem<span style="color:blue >
<span style="color:blue </<span style="color:#a31515 dummy_elem<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 Kates_elem<span style="color:blue />
<span style="color:blue </<span style="color:#a31515 dudes_elem<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 Toms_elem<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 Als_elem<span style="color:blue />
<span style="color:blue <<span style="color:#a31515 another_elem<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 some_elem<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 num1<span style="color:blue >14<span style="color:blue </<span style="color:#a31515 num1<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 num2<span style="color:blue >15<span style="color:blue </<span style="color:#a31515 num2<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 num3<span style="color:blue >13<span style="color:blue </<span style="color:#a31515 num3<span style="color:blue >
<span style="color:blue </<span style="color:#a31515 some_elem<span style="color:blue >
<span style="color:blue </<span style="color:#a31515 another_elem<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 Kates_elem<span style="color:blue />
<span style="color:blue <<span style="color:#a31515 sams_elem<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 item_elem<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 elem1<span style="color:blue >4<span style="color:blue </<span style="color:#a31515 elem1<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 elem2<span style="color:blue >3<span style="color:blue </<span style="color:#a31515 elem2<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 elem4<span style="color:blue >2<span style="color:blue </<span style="color:#a31515 elem4<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 elem6<span style="color:blue >1<span style="color:blue </<span style="color:#a31515 elem6<span style="color:blue >
<span style="color:blue </<span style="color:#a31515 item_elem<span style="color:blue >
<span style="color:blue </<span style="color:#a31515 sams_elem<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 your_elem<span style="color:blue />
<span style="color:blue </<span style="color:#a31515 Toms_elem<span style="color:blue >
<span style="color:blue </<span style="color:#a31515 my_elem<span style="color:blue >[/code]
I thought I could do it with LINQ to xml using something like this:<br/>

<pre style="font-family:Consolas; background-color:white <span style="color:#2b91af XDocument xmldoc = <span style="color:#2b91af XDocument.Load (<span style="color:red "update.xml");

IEnumerable<<span style="color:#2b91af XElement> elems = <span style="font-weight:bold; color:blue from el <span style="font-weight:bold; color:blue in xmldoc.Elements ()
<span style="font-weight:bold; color:blue orderby el.Name
<span style="font-weight:bold; color:blue select el;

xmldoc.ReplaceNodes (elems);
[/code]
but this returns the same exact doc as the original. What am I doing wrong? Please help.
Many Thanks.
Peter
<br/>


View the full article
 
Back
Top