how to sort nodes in the xml file and save file in sorted order using linq?
if this is my xml file,
<Persons>
<Person>
<id>2</id>
<Name>xxx</Name>
</Person>
<Person>
<id>3</id>
<Name>yyy</Name>
</Person>
</Persons>
when inserting new node.,
it should be inserted as like this.
<Persons>
<Person>
<id>1</id>
<Name>zzz</Name>
</Person>
<Person>
<id>2</id>
<Name>xxx</Name>
</Person>
<Person>
<id>3</id>
<Name>yyy</Name>
</Person>
</Persons>
how to do this using linq?
thank you
Something like the following. You should properly use int.TryParse instead of int.Parse and some other checks (for specific elements etc.) if your are not 100% sure of the input. Or check it against a schema.
(I have added a person in the end with id=1. I guess your forgot to add it.)
And to load/save your can use:
You can use:
instead of:
But again, check the elements if your not 100% sure of the input.