When I use LINQ to XML, is the order of the elements and attributes written out to text guaranteed to be the same order as how I added the XElement and XAttribute objects? Similarly, when I read in an XML document, is it traversed in the same order as it appears in the XML?
Asked
Active
Viewed 208 times
3 Answers
2
However, be aware that XML attributes are specified to be unordered; it would be unwise to rely on any particular ordering of attributes in any code. As a practical matter, many API's don't even make it discoverable in which order the attributes were
Sibling elements have order; attributes do not.

Eamon Nerbonne
- 47,023
- 20
- 101
- 166
1
Yes, it is the same order both ways.

R. Martinho Fernandes
- 228,013
- 71
- 433
- 510

James Alexander
- 6,132
- 10
- 42
- 56
1
Yes, you can use indexing as well, which maps directly to the order they appear in the document.

goldenratio
- 1,026
- 14
- 24