I have seen this similar question on sorting XElement
data (Sorting all the elements in a XDocument).
In my case I am only interested in the first level of children of the root node in the XDocument
being sorted. But, assume that the data is already sorted and I now want to add a new element. Eg:
<Root>
<A>...</A>
<C>...</C>
<D>...</D>
</Root>
So if I want to:
- Add an element called
B
I want to insert it afterA
. - Add an element called
E
it should be added to the end.
The latter is easy because we can simply Add
. Is there any way to insert the element at the right location automatically without resorting the whole list of nodes?
The suggested duplicate is not the same because they are stating where to put in. In my case we don't know. I have in mind a sortable collection where adding items is automatically sorted.