0

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 after A.
  • 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.

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
  • @KlausGütter No. In that scenario you are stating where to put in yourself. My scenario needs to work like a sorting container collection. – Andrew Truckle Jan 14 '22 at 08:19
  • 1
    There is nothing built-in, but it should be easy to first find the first element with tag > tag_to_be_inserted and then apply the method in the linked answer. – Klaus Gütter Jan 14 '22 at 08:30

0 Answers0